简体   繁体   English

如何从具有 python 文件列表的单个文件中打开多个文件以及如何对它们进行处理?

[英]how to open multiple file from single file which is having list of files in python and how to do processing on them?

i am having single file called bar.txt which is having list of files as follows,我有一个名为 bar.txt 的文件,它的文件列表如下,

bar.txt - bar.txt -

1.txt 1.txt

2.txt 2.txt

3.txt 3.txt

each file inside bar.txt having some similar contents. bar.txt 中的每个文件都有一些相似的内容。

1.txt - 1.txt -

spec = sadasdsad规格=sadasdsad

2.txt - 2.txt -

spec = dddddd规格 = dddddd

3.txt - 3.txt -

spec = ppppppppp规格 = ppppppppp

how can i open all files inside bar.txt and extract the data from all file and store in other file called foo.txt?如何打开 bar.txt 中的所有文件并从所有文件中提取数据并存储在另一个名为 foo.txt 的文件中?

in foo.txt i want extracted data mentioned below,在 foo.txt 我想要提取下面提到的数据,

foo.txt - foo.txt -

spec = sadasdsad规格=sadasdsad

spec = dddddd规格 = dddddd

spec = ppppppppp规格 = ppppppppp

 outfile = open('bar.txt', "rw")
 outfile_1 = open('foo.txt', "w")
     for f in outfile:
        f=f.rstrip()
        lines = open(f,'rw')
        lines = re.findall(".*SPEC.*\\n",lines)
        outfile_1.write(lines)
 outfile.close()

I'd do this:我会这样做:

infile = open('bar.txt', "r")
outfile = open('foo.txt', "w")
line = infile.readline()

while line:
    f = line.rstrip()
    contents_file = open(f,'rw')
    contents = contents_file.read()
    outfile.write(contents)
    f = infile.readline()
 
outfile.close()

You're code is almost correct.你的代码几乎是正确的。 I guess you messed everything up by using f variable for almost everything.我猜您几乎对所有内容都使用了f变量,从而搞砸了一切。 So you assign multiple different things to one f variable.因此,您将多个不同的东西分配给一个 f 变量。 First its single line on outfile, then it's the same line striped, then it's another opened file, and finally you try to use the same f variable outside of it's scope (outside for loop).首先它在 outfile 上的单行,然后是同一行条纹,然后是另一个打开的文件,最后你尝试在它的 scope 之外使用相同的 f 变量(在 for 循环之外)。 Try using different variables for all theses beings.尝试对所有这些存在使用不同的变量。

Also make sure you have correct indents (eg for loop indent is incorrect in your example), and not that regex findall works on string, not file-like object, so second argument of findall should be contentfile.read() .还要确保您有正确的缩进(例如for loop indent 在您的示例中不正确),而不是正则表达式findall适用于字符串,而不是类似文件的 object,因此findall的第二个参数应该是contentfile.read()

infile = open('bar.txt', "r")
outfile = open('foo.txt', "w")
for f in infile:
    name=f.rstrip()
    contentfile = open(name,'rw')
    #all_matches= re.findall(<define your real pattern here>,contentfile.read())
    result = 0 #do something with your all_matches
    outfile.write(result)
    contentfile.close()
outfile.close()
infile.close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从文件夹中打开多个 JSON 文件并将它们合并到 python 中的单个 JSON 文件中? - How to open multiple JSON file from folder and merge them in single JSON file in python? 如何在python中安全地将单个hdf5文件中的数据并行写入多个文件? - how do I safely write data from a single hdf5 file to multiple files in parallel in python? 如何从文件路径列表中打开文件 - How to open files from a list of file paths 如何将 .txt 文件中的列表转换为 Processing (python) 中的列表? - How do I convert a list in a .txt file to a list in Processing (python)? 如何从文件对话框中选择多个文件并同时打开并访问它们 - How to choose multiple files from file dialog and open at the same time and access them 如何在Windows上为python打开文件? 遇到意外问题 - How do you open a file on windows for python? having unexpected issues 如何将多个.pcd 文件组合成一个包含点云数据(python)的单个.pcd 文件? - How to combine multiple .pcd files into a single .pcd file which contains point cloud data (python)? 如果我有一个CSV文件的Python列表,如何将它们全部合并为一个巨型CSV文件? - If I have a Python list of CSV files, how do I merge them all into one giant CSV file? 每次使用pandas和python处理多个文件时,从txt文件中读取单个变量 - Read single varible from txt file each time processing multiple files using pandas and python 如何在python中打开多个doc文件并从中解析电子邮件ID并写入csv? - How to open multiple doc file in python and parse email ID from them and write in csv?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM