gpt4 book ai didi

python - 如何循环导入多个 .txt 文件但不串联?

转载 作者:行者123 更新时间:2023-12-01 00:41:07 24 4
gpt4 key购买 nike

我在 .txt(以空格分隔)中有许多(>40)数据文件,它们具有相同的布局,我想将它们读入 python 中进行数据处理和绘图。这些文件是一个参数的参数化扫描的模型输出,该参数在每个数据文件中占据一列。该参数递增到每个连续文件中的下一个值。

我遇到的问题是我不知道如何编写 for 循环来将每个数据文件读取到自己的数据帧中。

我看到很多答案建议“pandas.read_csv”然后串联,但是我不想将文件连接到一个数据帧中,因为我想单独绘制每个数据集。对我来说,仅仅为了随后分离出数据集而诉诸连接数据框是没有意义的。

import glob
import os
import pandas as pd
from pandas import Series, DataFrame

path = r'D:/user/data-folder/'

files = glob.glob(os.path.join(path + 'data-*.txt')) # Added based on suggestions from similar questions
df1 = []
for f in files:
df = pd.read_csv(path1 + f,
sep=' '
)
df1.append(df)

print(df1)

理想情况下,我希望将每个数据文件读入自己的数据帧中,并递增编号,例如“df1_1”、“df1_2”等然后,我可以单独操作每个数据帧,并将数据相互绘制以进行比较。

最佳答案

数据框列表怎么样?如果您有:

../data/a.txt:

firstname,lastname,hobby
niles,crane,wine tasting
martin,crane,sitting in recliner
bob,bulldog,being annoying

../data/b.txt:

firstname,lastname,hobby
john,doe,doing stuff
jane,doe,being anonymous
humphrey,bogart,smoking and drinking

代码:

def main():

from glob import glob
from os.path import join
import pandas as pd
from pandas import DataFrame
from contextlib import ExitStack

local_path = "data/"

filenames = glob(join(local_path + "*.txt"))

with ExitStack() as context_manager:
files = [context_manager.enter_context(open(filename, "r")) for filename in filenames]

dataframes = []
for file in files:
dataframe = pd.read_csv(file)
dataframes.append(dataframe)

print(dataframes[0], end="\n\n")
print(dataframes[1])

return 0


if __name__ == "__main__":
import sys
sys.exit(main())

输出:

  firstname lastname                hobby
0 niles crane wine tasting
1 martin crane sitting in recliner
2 bob bulldog being annoying

firstname lastname hobby
0 john doe doing stuff
1 jane doe being anonymous
2 humphrey bogart smoking and drinking

关于python - 如何循环导入多个 .txt 文件但不串联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57339896/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com