我只想显示第二列,
这是我的代码
with open('try.txt', 'rb') as file1:
file1_data = dict(line.split(None, 2)[1] for line in file1 if line.strip())
print file1_data
这是文本文件
E5 Bat One
E5 BALL Two
E5 CRICKET Three
E5 Bat03 Four
E5 Bat133 Five
比我必须找到第二个文本文件中 textfile1 第二列中的蝙蝠
Secondtextfile
?Bat03|Batsman
This is the goods of cricket
Usually cricketers use it
it is game
?Balt|Bowler
Both can be use by batsman and bowler
?Bat133|Swimmer
Swiming is the best exercise
textfile1
因此,我们转到第二列中的 Batsecond text file
并找到 Bat03 和 Bat133 并忽略之后的值|
,如果Bat
在找到之前的第二个文本文件中,则|
显示它
with open('second.txt', 'rb') as file2, open('output.txt', 'wb') as outputfile:
output = csv.writer(outputfile, delimiter='|')
for line in file2:
if line[:1] == '?':
row = line.strip().split('|')
key = row[0][1:]
if key in file1_data:
output.writerow(row + [file1_data])
else:
outputfile.write(line)
所需输出
?Bat03|Batsman
This is the goods of cricket
This is the goods of cricket
Usually cricketers use it
it is game
?Bat133|Swimmer
Swiming is the best exercise