Python 迭代两个文件,比较行中的匹配字符串,合并匹配行
我有两个文件,里面有一份生物体的清单。第一个文件包含一个指示“家族属”的列表,因此有两列。第二个文件包含“属-种”,也有两列。这两个文件在所有列出的物种的属上是一致的。我想使用每个文件的属合并这两个列表,以便能够将科名称添加到“属物种”中。因此,输出应包含“科-属-种”。由于每个名称之间都有一个空格,所以我使用该空格将其拆分为列。到目前为止,这是我的代码:Python 迭代两个文件,比较行中的匹配字符串,合并匹配行,python,regex,python-3.x,Python,Regex,Python 3.x,我有两个文件,里面有一份生物体的清单。第一个文件包含一个指示“家族属”的列表,因此有两列。第二个文件包含“属-种”,也有两列。这两个文件在所有列出的物种的属上是一致的。我想使用每个文件的属合并这两个列表,以便能够将科名称添加到“属物种”中。因此,输出应包含“科-属-种”。由于每个名称之间都有一个空格,所以我使用该空格将其拆分为列。到目前为止,这是我的代码: with open('FAMILY_GENUS.TXT') as f1, open('GENUS_SPECIES.TXT') as f2:
with open('FAMILY_GENUS.TXT') as f1, open('GENUS_SPECIES.TXT') as f2:
for line1 in f1:
line1 = line1.strip()
c1 = line1.split(' ')
print(line1, end=' ')
for line2 in f2:
line2 = line2.strip()
c2 = line2.split(' ')
if line1[1] == line2[0]:
print(line2[1], end=' ')
print()
结果输出仅由两行组成,而不是整个记录。我错过了什么
还有,我怎样才能把它保存到一个文件中,而不是仅仅在屏幕上打印?我会先处理这些文件,然后得到它可能包含的属到科和多个物种的映射。然后使用该映射将它们匹配并打印出来
genuses = {}
# Map all genuses to a family
with open('FAMILY_GENUS.TXT') as f1:
for line in f1:
family, genus = line.strip().split()
genuses.setdefault(genus, {})['family'] = family
# Map all species to a genus
with open('GENUS_SPECIES.TXT') as f2:
for line in f2:
genus, species = line.strip().split()
genuses.setdefault(genus, {}).setdefault('species', []).append(species)
# Go through each genus and create a specie string for
# each specie it contains.
species_strings = []
for genus, d in genuses.items():
family = d.get('family')
species = d.get('species')
if family and species:
for specie in species:
s = '{0} {1} {2}'.format(family, genus, specie)
species_strings.append(s)
# Sort the strings to make the output pretty and print them out.
species_strings.sort()
for s in species_strings:
print s
这是另一种解决办法
f1 = open('fg','r')
f2 = open('gs','r')
genera= {}
for i in f1.readlines():
family,genus = i.strip().split(" ")
genera[genus] = family
for i in f2.readlines():
genus,species = i.strip().split(" ")
print(genera[genus], genus,species)