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?

martineau
124k29 gold badges181 silver badges319 bronze badges
asked Jul 29, 2018 at 19:21
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)
answered Jul 29, 2018 at 19:27
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

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.