我有这个问题。我有两个文件.txt,match_list
这种形式:
Sevilla, Ath Bilbao
Valencia, Valladolid
Getafe, Atl. Madrid
和data
Getafe, Atl. Madrid,5:00 PM, Coliseum Alfonso Pérez, 9.500,27.8, 2.760.000,8
Valencia, Real Valladolid,7:00 PM, Mestalla, 41.000,26.3, 8.640.000,6
Sevilla, Ath Bilbao,8:00 PM, Ramón Sánchez Pizjuan, 4-2-3-1,26, 5.488.000,11
我想循环浏览两个文件,匹配团队名称并将与data.txt
到match_list.txt
的每个匹配项对应的所有数据复制。
我的代码无法处理这个问题(也许是因为我只通过一个文件循环?):
match_data = open('data.txt').readlines()
match_list = open('m_list.txt').readlines()
outfile = open('done.txt', 'w')
for line in match_data:
if line[:2] == match_list[:2]:
match_list = match_list+',' + line[2:]
outfile.write(match_list)
答案 0 :(得分:2)
您有CSV数据,请使用csv
模块阅读它们。首先将data.txt
读入内存,进入前两列的字典:
import csv
with open('data.txt', 'rb') as match_data:
reader = csv.reader(match_data)
match_data = {tuple(row[:2]): row for row in reader}
with open('m_list.txt', 'rb') as match_list, open('done.txt', 'wb') as outfile:
reader = csv.reader(match_list)
writer = csv.writer(outfile)
for row in reader:
row = tuple(row)
if row in match_data:
writer.writerow(match_data[row])
答案 1 :(得分:1)
试试这个:
for line in match_list:
for x in match_data:
if x.startswith(line.split(',')[0]):
outfile.write(line +', '+ x)
break
我没有对每个字符串进行任何格式化,如果您愿意,可以执行此操作。但是因为大概两个文件中的顺序不一样,你需要执行一个匹配(如果你把它们读成真正的数据结构会更容易 - 如果你愿意,可以试试csv
),或者只是在两个列表上循环,假设它们不是太大。
答案 2 :(得分:1)
您的data.txt是否已经采用您希望match_list.txt所在的格式?
for line in match_data :
for match in match_list :
if match[:-1] in line : # careful of \n
# write line to file
break
答案 3 :(得分:1)
对不起。我在你的问题中看到了“批处理文件”标签,所以我发布了批处理文件.bat解决方案作为替代方案。我的方法包括首先将data
文件的所有行加载到由团队名称索引的数组中;这样,当处理match_list
文件时,可以直接访问data
的相应元素。这是:
@echo off
setlocal EnableDelayedExpansion
rem Load all lines of data.txt file into data array indexed by team name
for /F "tokens=1* delims=," %%a in (data.txt) do (
set data[%%a]=%%b
)
rem Process lines of m_list.txt file and output the corresponding data into done.txt file
(for /F "delims=," %%a in (m_list.txt) do (
echo %%a, !data[%%a]!
)) > done.txt