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
1 Answer 1
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
taras
6,93510 gold badges46 silver badges54 bronze badges
lang-py
reshapeline? Did you checkself.inputTotalDatabefore that?