一次性写入多个文件

5

我有一个包含196个列表的文件,我想创建196个新的输出文件,并将每个列表写入一个新文件中,这样我将有196个输出文件,每个文件包含1个输入数据列表。

以下是输入文件:

"['128,129', '116,118', '108,104', '137,141', '157,144', '134,148', '138,114', '131,138', '248,207', '208,247', '246,248', '101,106', '131,115', '119,120', '131,126', '138,137', '132,129']"
"['154,135', '151,147', '236,244', '243,238', '127,127', '125,126', '122,124', '123,126', '127,129', '122,121', '147,134', '126,132', '128,137', '233,222', '222,236', '125,126']"

这里举例只给出了2个列表,但总共有196个列表。

输出应为:

文件1:

128,129
116,118
108,104

文件2:

154,135
151,147
236.244

现有代码:

fn = open("/home/vidula/Desktop/project/ori_tri/inpt.data","r")
fnew = fn.read()
fs = fnew.split('\n')
for value in fs:
    f = [open("/home/vidula/Desktop/project/ori_tri/input_%i.data" %i,'w') for i in range(len(list_of_files))]
    f.write(value)
    f.close()

错误:列表无法写入属性。

由于Stackoverflow不会为您编写代码:您自己尝试了什么?另请参阅FAQ - user707650
1
除了解释你想要实现什么,还要展示你已有的代码,并解释你遇到的问题。 - Lennart Regebro
fn = open("/home/vidula/Desktop/project/ori_tri/inpt.data","r") fnew = fn.read() fs = fnew.split('\n') for value in fs: f = [open("/home/vidula/Desktop/project/ori_tri/input_%i.data" %i,'w')for i in range(len(list_of_files))] f.write(value) f.close() 错误:列表没有write属性。 - shreya
这个列表不能被写入输出文件。 - shreya
我已经将你评论中的代码添加到了你的问题中,这样更容易阅读,人们现在可以(轻松地)建议修复你的代码。 - user707650
5个回答

11

您当前的代码将所有内容都加载到内存中,这是非常不必要的,然后在不合适的位置创建列表,因此会出现错误。请尝试以下代码:

fn = open("/home/vidula/Desktop/project/ori_tri/inpt.data","r")
for i, line in enumerate(fn):
    f = open("/home/vidula/Desktop/project/ori_tri/input_%i.data" %i,'w')
    f.write(line)
    f.close()

这将只是将每行内容原封不动地写入每个文件。查阅我用于索引的枚举函数

完成此操作后,您仍需要编写解析逻辑,将每行转换为一系列行...我不会在此为您编写代码,因为您的原始代码实际上也没有相关逻辑。


1
你无法让Python在列表类中查找可迭代的写入对象,因为列表与write()方法不兼容。在Python中,列表是通过追加实现的。
假设你的数据文件已经有了换行符,请创建一个过滤器对象来删除空行,然后进行迭代:
string1 = '128,129', '134, 167', '127,189'
string2 = '154, 134', '156, 124', '138, 196'
l1 = list(string1)
l2 = list(string2)
data = [l1, l2]
f = open("inpt.data", "w")
for val in data:
    f.write(str(val))
    f.write('\n')
f.close()

with open("inpt.data", "r", encoding='utf-8') as fs:
    reader = fs.read()
    file = reader.split('\n')
    file = filter(None, file)

最简单的方法:

# create one file for each list of data (1) on every new line 
i = 0
for value in file:
    i += 1
    f = open("input_%s.data" % i, 'w')
    f.write(value)
fs.close()

Pythonic的简单方法:

for i, line in enumerate(file):
    fi = open("input_%s.data" % i, 'w')
    fi.write(line)
fs.close()

1

你的 f 是一个文件列表,你需要循环遍历它:

for file in f:
   file.write(value)

尝试过程中出现错误:Traceback (most recent call last): File "/home/vidula/Desktop/project/try_brain-input.py", line 38, in <module> f.write(value) AttributeError: 'list' object has no attribute 'write' - shreya
不,你还没有尝试 :) 你仍然有"f.write",但是你必须遍历该列表的元素(假设f仍然是使用您的初始生成器表达式创建的)。 - Samuele Mattiuzzo

0

我假设您想要读取196个文件,然后将数据(经过一些修改)写入新的196个文件。如果您使用映射和归约(函数式编程),它可以实现您想要的功能。但是,由于问题中没有太多解释,我无法提供太多帮助。

def modify(someString):
    pass # do processing

def newfiles(oldfilename): return '%s.new.txt'%(oldfilename) # or something 

filenames = ('a', 'b', 'c', 'd', ....) 
handles = [(open(x, 'r'), open(newfile(x), 'w')) for x in filenames] # not using generator
tmp = [y[1].write(modify(y[0].read())) for y in handles) 

@ sam。我刚看到了这个错误(单引号和双引号),在我来得及修改之前,你已经修改了它。谢谢。 - pranshus
1
“Lambda编程”不是您要找的术语。我猜您想说的是“函数式编程”。 - Alex V
@ max。是的,我想说的是函数式编程。已更正。谢谢。 - pranshus
我需要从单个文件生成多个文件。我的输入文件是以读取模式打开的,我需要从输入文件生成196个文件。我的输入文件包含如上所示的196个列表,我想要创建196个输出文件,每个文件都包含输入文件中的一个列表,简而言之,将输入文件中的列表分发到多个输出文件中。 - shreya
@vids。我认为约翰的答案会对你有所帮助。如果不行,请在问题中更具体地说明。 - pranshus

0

我认为这就是您要找的内容:

with open("/home/vidula/Desktop/project/ori_tri/inpt.data","r") as fn:
    listLines = fn.readlines()

for fileNumber, line in enumerate(listLines):
    with open("/home/vidula/Desktop/project/ori_tri/input{0}.data".format(fileNumber), "w") as fileOutput:
        fileOutput.write(line)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接