1

I have 3 different .txt files and each of them contains x,y,z coordinates such: I read the content of those 3 files:

inputFileList = sorted(glob.glob(inputSourceDir + '/*.txt'), key=lambda x: (int(re.sub('\D', '', x)), x))
inputFileList = inputFileList[0:100]
inputTotalDataList = []
self.numberOfInputFiles = 0

for inputFilePath in inputFileList:
    inputDataInFile = np.genfromtxt(inputFilePath, dtype=float, delimiter=',')  # usecols= 0

    baseWithExt = os.path.basename(inputFilePath)
    base = os.path.splitext(baseWithExt)[0]

    inputTotalDataList.append(inputDataInFile)

    self.numberOfInputFiles = self.numberOfInputFiles + 1

    self.inputTotalData = np.array(inputTotalDataList)
    self.inputTotalData = self.inputTotalData.reshape(self.numberOfInputFiles * len(inputDataInFile), 3)
    print('TotalData: ', inputTotalData )

As output I get:

TotalData:  [[   7.29948 -187.854    760.208  ]
 [ -41.2607  -188.068    761.008  ]
 [ -13.2162  -193.675    771.235  ]
 [  35.361   -185.632    776.405  ]
 [ -58.8706  -188.025    785.184  ]
 [  12.8998  -196.275    789.446  ]
 [ -27.303   -198.127    791.598  ]
 [ -48.8703  -195.487    812.969  ]
 [  30.4976  -192.05     818.794  ]]

But I want to represent each read file like:

[[ 7.29948 -187.854    760.208  ]
 [ -41.2607  -188.068    761.008  ]
 [ -13.2162  -193.675    771.235  ]]
[[ 35.361   -185.632    776.405  ]
 [ -58.8706  -188.025    785.184  ]
 [  12.8998  -196.275    789.446  ]]
[[ -27.303   -198.127    791.598  ]
 [ -48.8703  -195.487    812.969  ]
 [  30.4976  -192.05     818.794  ]]

. . .

How can I convert my output to the desired result above?

1
  • Is your indentation off? (e.g. the last 3 lines?). What's the purpose of the reshape line? Did you check self.inputTotalData before that? Commented Jul 29, 2018 at 20:47

1 Answer 1

2

Instead of

self.inputTotalData = self.inputTotalData.reshape(self.numberOfInputFiles * len(inputDataInFile), 3)

use the desired inner shape (3x3) and let it deduce the first (outer) dimension

self.inputTotalData = self.inputTotalData.reshape(-1, 3, 3)
Sign up to request clarification or add additional context in comments.

2 Comments

Can I also convert that self.inputTotalData np.array to dict after reshaping?
Yes, you can. Assuming there is a list of keys, it can be done with dict(zip(keys, self.inputTotalData))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.