Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

python - How to iteratively extract a column from text files to put into a 2D array

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Found a work around creating a 3D array instead, much better imo

alist = []
for f in filenames_discs:
    alist.append(np.loadtxt(f))

arr = np.array(alist,dtype=object)
print(arr[1][0,0])
print(arr.shape)    

where arr is the array of all disc data.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...