[英]How to take a column from a txt file and save in a new matrix
I did this code to go through a folder, find all .txt files and take the 4th column from this .txt file (has a lot of columns) and put in a new numpy array (data) 我执行此代码来浏览文件夹,找到所有.txt文件,并从该.txt文件中提取第四列(有很多列),然后放入新的numpy数组(数据)
import numpy as np
from scipy.constants import mu_0
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
import pandas as pd
data=np.zeros((44,14)) # there are 14 .txt files and the 4th column has 44 lines
indx = 0
import os
Path = "my path"
filelist = os.listdir(Path)
for i in filelist:
if i.endswith(".txt"):
newpath = Path+ '/'+i
print(newpath) # check if the path and file is right
dados= pd.read_table(newpath,header=None)
data[:,indx] = dados[:][4]
indx = indx+1
the error I'm getting is: First: I have some problem with index, because is starting at 1 and should be at 0. Second: Is just taking the 4th column from the first .txt file and putting in the array data, but then stops and do not run through the other files. 我得到的错误是:第一:索引有问题,因为它从1开始,应该为0。第二:只是从第一个.txt文件中提取第4列并放入数组数据,但是然后停止,不要运行其他文件。
This is the errror: ParserError: Error tokenizing data. 这是错误:ParserError:对数据进行令牌化时出错。 C error: Expected 5 fields in line 49, saw 7
C错误:第49行中应有5个字段,看到7
Try this : 尝试这个 :
import os
import pandas as pd
workingpath = os.getcwd()
files = []
for file in os.listdir(workingpath):
if file.endswith(".txt"):
files.append(os.path.join(workingpath,file))
data = pd.DataFrame()
for col, file in enumerate(files):
dados = pd.read_csv(file, header=None)
data[col] = dados.iloc[:,4]
data = data.to_numpy()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.