我有以下两个文件。
我试图为此编写一个python脚本,但我不知道如何制定它。
File 1:
a2 sca4 15 20
b3 sca4 22 30
c4 sca6 45 65
File 2:
sca4 17
sca4 18
sca4 19
sca6 46
output:
a2 sca4 17 15 20
a2 sca4 18 15 20
a2 sca4 19 15 20
c4 sca6 46 45 65
答案 0 :(得分:2)
我会使用Math已经发布的解决方案,除了使用with
语句进行文件读取。
file2_dict = {}
with open("file2.txt") as fo:
file_contents = fo.read()
for line in file_contents.split("\n"):
if not line.strip():
continue
key, value = line.strip().split(" ", 1)
if key not in file2_dict:
file2_dict[key] = []
file2_dict[key].append(int(value))
output_string = ""
with open("file1.txt") as fo:
file_contents = fo.read()
for line in file_contents.split("\n"):
if not line.strip():
continue
name, id, min, max = line.strip().split(" ")
for value in file2_dict.get(id, []):
if int(min) < value < int(max):
output_string += line.replace(id, "%s %d" % (id, value))
output_string += "\n"
print output_string
答案 1 :(得分:1)
尝试这样的事情:
import sys
def process_files(one, two):
for line in [line.strip().split(" ") for line in open(two, 'r').readlines()]:
for x in filter(lambda x: x[1] == line[0], [z.strip().split(" ") for z in open(one, 'r').readlines()]):
if int(x[2]) <= int(line[1]) <= int(x[3]):
print(" ".join(x))
if __name__ == "__main__":
process_files(sys.argv[1], sys.argv[2])
答案 2 :(得分:1)
我会使用dict
来获取文件2中的每个索引,然后浏览文件1.
ff = file("file2", "r")
file2 = ff.readlines()
ff.close()
dict_f2 = dict()
for line in file2:
ll = line.strip().split(" ")
if (ll[0] in dict_f2):
dict_f2[ll[0]].append(int(ll[1]))
else:
dict_f2[ll[0]] = list()
dict_f2[ll[0]].append(int(ll[1]))
ff = open("file1", "r")
file1 = ff.readlines()
ff.close()
for line in file1:
ll = line.strip().split(" ")
if (ll[1] in dict_f2):
sup = int(ll[4])
inf = int(ll[3])
for (comp in dict_f2[ll[1]]):
if (comp >= inf and comp <= sup):
print(line[0:2] + str(comp) + line[2:])