将文件中的新字符串逐行添加到新文件中



我运行的脚本中有一个格式如下的数据输出文件。

1. xxx %percentage1
2. yyy %percentage1
.
.
.

我试着只取百分比,并逐行将它们附加到相同格式的文件中(在此过程中写一次新文件(。

1. xxx %percentage1 %percentage2
2. yyy %percentage1 %percentage2

主要的想法是,每次我用源数据文件运行代码时,我都希望它逐行将这些百分比添加到新文件中。

1. xxx %percentage1 %percentage2 %percentage3 ...
2. yyy %percentage1 %percentage2 %percentage3 ...

这就是我能想到的:

import os
os.chdir("directory")
f = open("data1", "r")
n=3
a = f.readlines()
b = []
for i in range(n):
b.append(a[i].split(" ")[2])
file_lines = []
with open("data1", 'r') as f:
for t in range(n):
for x in f.readlines():
file_lines.append(''.join([x.strip(), b[t], 'n']))
print(b[t])
with open("data2", 'w') as f:
f.writelines(file_lines)

有了这段代码,我得到了新文件,但追加的百分比都来自第一行,每行都没有不同。我只能添加一组百分比,它会覆盖它,而不是添加更多的百分比。

我希望我解释得很好,如果你能帮忙,我会很高兴的。

您可以使用dict作为结构来加载和写入数据。然后可以对这个dict进行pickle以存储数据。

EDIT:添加了缺失的返回语句

编辑2:修复get_data 的返回列表

import pickle
import os
output = 'output'
dump = 'dump'
output_dict = {}
if os.path.exists(dump):
with open(dump, 'rb') as f:
output_dict = pickle.load(f)
def read_data(lines):
""" Builds a dict from a list of lines where the keys are
a tuple(w1, w2) and the values are w3 where w1, w2 and w3
are the 3 words composing each line.
"""
d = {}
for line in lines:
elts = line.split()
assert(len(elts)==3)
d[tuple(elts[:2])] = elts[2]
return d
def get_data(data):
""" Recover data from a dict as a list of strings.
The formatting for each element of the list is the following:
k[0] k[1] v
where k and v are the key/values of the data dict.
"""
lines = []
for k, v in data.items():
line = list(k)
line += [v, 'n'] 
lines.append(' '.join(line))
return lines
def update_data(output_d, new_d):
""" Update a data dict with new data
The values are appended if the key already exists.
Otherwise a new key/value pair is created.
"""
for k, v in new_d.items():
if k in output_d:
output_d[k] = ' '.join([output_d[k], v])
else:
output_d[k] = v
for data_file in ('data1', 'data2', 'data3'):
with open(data_file) as f:
d1 = read_data(f.readlines())
update_data(output_dict, d1)
print("Dumping data", output_dict)
with open(dump, 'wb') as f:
pickle.dump(output_dict, f)
print("Writing data")
with open(output, 'w') as f:
f.write('n'.join(get_data(output_dict)))

最新更新