I have a set of a few hundred 2D text files each with 37 columns (each for a specific quantity e.g. time, mass, ang mom, etc.) but differing row length. I would like to extract a specific column from each text file into an array so it contains this data for all text files, say the first column of each text file into an array.
The files take names as such:
'Disc_001_25451941.txt',
'Disc_002_07305488.txt',
.
.
.
,
'Disc_140_06782432.txt'
Firstly, I have sorted the filenames so I know how the files will appear in the idisc_data array. I have tried using the following code to extract just the first column from each text file an place into a 2D array:
import numpy as np
import matplotlib.pyplot as plt
from glob import glob
import re
filenames_discs = glob('~/Disc_*.txt')
#utime=4.703706983e05
def tryint(s):
try:
return int(s)
except ValueError:
return s
def alphanum_key(s):
return [tryint(c) for c in re.split('([0-9]+)', s)]
def sort_nicely(l):
return sorted(l, key=alphanum_key)
filenames_discs=sort_nicely(filenames_discs)
gt=[]
for i in range(len(filenames_discs)):
idisc_data = np.loadtxt(filenames_discs[i])
gt[:,i] = idisc_data[:,0]
This results in the error TypeError: list indices must be integers or slices, not tuple
. How do I fix this error, or what is the correct way of extracting each column to put into a 2D array.
question from:
https://stackoverflow.com/questions/66045968/how-to-iteratively-extract-a-column-from-text-files-to-put-into-a-2d-array 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…