Python将文件拆分为多个文件并添加额外信息
我希望拆分2个文本文件并将其合并为: 因此,第一个文件名为“Names.txt”,是一个名称列表。它非常大,因此手动将名称放入下一部分是不可行的:Python将文件拆分为多个文件并添加额外信息,python,text,split,writing,Python,Text,Split,Writing,我希望拆分2个文本文件并将其合并为: 因此,第一个文件名为“Names.txt”,是一个名称列表。它非常大,因此手动将名称放入下一部分是不可行的: Chloe Megan Harry etc... 第二个文件名为“Attributes.txt”,是由$$$分隔的属性集列表: attribute1 attribute2 attribute3 $$$$ attribute1 attribute2 etc... 一组属性,每个属性都与第一个文件中的名称相关。但是我不知道每个集合中有多少属性,因为它
Chloe
Megan
Harry
etc...
第二个文件名为“Attributes.txt”,是由$$$分隔的属性集列表:
attribute1
attribute2
attribute3
$$$$
attribute1
attribute2
etc...
一组属性,每个属性都与第一个文件中的名称相关。但是我不知道每个集合中有多少属性,因为它是随机的
我想从第一个文件中获取第一个名称,从第二个文件中获取第一组属性,并将它们写入新文件:
Chloe
attribute1
attribute2
attribute3
然后循环它,这样它就可以得到第二个名称和集合,依此类推
到目前为止,我有以下代码:
import os
input_file1 = open('Names.txt', 'r')
input_file2 = open('Attributes.txt', 'r')
lines1 = input_file1.readlines()
def group_by_person(some_source):
buffer = []
for line in (some_source):
if line.startswith("$$$$"):
if buffer: yield buffer
buffer = [line]
else:
buffer.append(line)
yield buffer
i = 0
name1 = lines1[i]
name2 = name1[:-1]
g = 0
while os.path.exists(name2 + '%s.txt' % g):
g += 1
with open(name2 + '%s.txt' % g, 'w') as f:
with input_file2 as source:
for lines2 in group_by_name(source):
f.write(lines2[i])
i += 1
有人能帮忙吗?我想这就是你想要达到的目的,如果我错了,请评论:
def group_by_person(names_file, attributes_files):
with open(names_file) as names, open(attributes_files) as attributes:
for name in names:
line = [name.strip()]
for attribute in attributes:
if attribute.startswith("$$$$"):
break
line.append(attribute.strip())
print line
yield line
names_count = {}
for name in group_by_person('Names.txt', 'Attributes.txt'):
n = name[0]
names_count[n] = names_count.setdefault(n, 0) + 1
with open("%s%s.txt" % (n, names_count[n]), 'w') as f:
f.write('\n'.join(name))
测试结果:
Names.txt:
Chloe
Megan
Chloe
Attributes.txt:
attribute1
attribute2
attribute3
$$$$
attribute4
attribute5
$$$$
attribute6
外部文件:
Chloe1.txt, Megan1.txt, Chloe2.txt
Chloe1:
Chloe
attribute1
attribute2
attribute3
Megan1.txt
Megan
attribute4
attribute5
Chloe2.txt:
Chloe
attribute6
我相信这是决定性的
最终编辑。您的代码很难阅读和理解。尝试将其拆分为逻辑部分。您使用generator按人分组,这是一个很好的解决方案。如何创建相同的生成器来获取人员?之后,您可以使用函数聚合人员和组。我的意思是:
def persons():
with open('Names.txt', 'r') as f:
for line in f:
line = line.rstrip()
if line: yield line
def groups():
with open('Attributes.txt', 'r') as f:
group = []
for line in f:
line = line.rstrip()
if line == '$$$$':
if group: yield group
group = []
else:
group.append(line)
if group: yield group
for person, group in zip(persons(), groups()):
print(person, group)
输出:
Chloe ['attribute1', 'attribute2', 'attribute3']
Megan ['attribute4', 'attribute5']
Harry ['attribute6', 'attribute7', 'attribute8']
现在,所有的任务都是将其写入文件并检查案例,而不是每个人都获得了一组属性。这样如何:
with open("Names.txt") as namefile, open("Attributes.txt") as attfile:
names = namefile.read().split("\n")
attributes = attfile.read().split("\n$$$$\n")
pairs = list(zip(names, attributes))
现在,每一对都将一个名称与相应位置的属性相关联,您可以进一步处理该名称,以按照自己的喜好对其进行格式化。欢迎使用SO。这是一个很好的质量问题!只缺少一件事:运行代码时会发生什么。显然,它不会生成所需的输出,否则您不会询问,但它是否会生成错误的输出,崩溃或烧坏?:)嗨,我得到一个错误“索引器:列表超出范围”。它确实使用正确的名称编写了一个新文件,但具有第二个人的属性,并且它根本不循环。您的错误是什么?如果是崩溃,您通常会得到大量调试信息(例如堆栈跟踪),这些信息对查找错误非常有帮助。加入并写出。查找它们。这不是您正在运行的代码。您的函数名为
groupby\u person
,但您调用的函数名为groupby\u name
。请复制并粘贴您的实际代码。谢谢。这不是很近。OP不会在groupby\u persons
中硬编码文件名,OP也担心输出文件中的名称冲突。Ye好吧,我相信他可以自己更改函数来添加额外的参数。。。这不是真正的点,当您将迭代编号添加到文件名时,输出文件不会发生冲突。(除非您以批处理方式运行,否则您需要一个更强大的系统或记住上一次糟糕的迭代编号。)OP的代码具有冲突名称“uniquifiers”,每个名称前进1,但您已将迭代编号添加到所有名称中。非常感谢Maresh!像做梦一样工作!但这还不完全,他还有其他顾虑。这显然值得投反对票。。。你不会从我这里得到的。@Maresh编写最少的代码很容易,但我们不应该帮助他解决问题,而不是代替他解决问题吗?我同意。我试过了。被否决的选票。。。将向上移动u;-)