0

我做了这段代码来遍历一个文件夹,找到所有 .txt 文件并从这个 .txt 文件中获取第 4 列(有很多列)并放入一个新的 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 

我得到的错误是:第一:我的索引有问题,因为从 1 开始,应该是 0。第二:只是从第一个 .txt 文件中取出第 4 列并放入数组数据,但是然后停止并且不运行其他文件。

这是错误:ParserError:错误标记数据。C 错误:预计第 49 行中有 5 个字段,看到 7

4

1 回答 1

1

尝试这个 :

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()
于 2019-09-18T17:18:25.583 回答