1

I have this problem. I have two files .txt, match_list of this form:

Sevilla, Ath Bilbao
Valencia, Valladolid
Getafe, Atl. Madrid 

and 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

I want to loop through both files, match team names and copy all the data corresponding to each match from data.txt to match_list.txt.

My code couldn't handle this (maybe because i'm looping only through one file?):

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)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
nutship
  • 4,624
  • 13
  • 47
  • 64

4 Answers4

2

You have CSV data, use the csv module to read them. Read data.txt into memory first, into a dictionary keyed on the first two columns:

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])
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks once more for this this beautiful solution. Did you try to run this code successfully yourself maybe? Cuz I got an error `if row in match_data:` `TypeError: unhashable type: 'list'` Sorry for bothering but this csv module is yet over my head and can't figure out whats wrong here. – nutship Apr 12 '13 at 16:48
  • 1
    Corrected; I forgot to turn the `list` row into a `tuple` in the second stage. – Martijn Pieters Apr 12 '13 at 16:50
  • Excuse me for post this unrelated comment here, but I can't found any other method to contact you. I just realized that you deleted [this answer](https://stackoverflow.com/a/23956113/778560) of mine that have been active for more than 5 years! May I ask you the reason for that? Thanks... – Aacini Apr 07 '21 at 00:32
  • @Aacini: please use flagging for that in future. We cleaned up several answers to that post that were either link-only or not really answering the question. – Martijn Pieters Apr 07 '21 at 08:47
1

Try this:

for line in match_list:
    for x in match_data:
        if x.startswith(line.split(',')[0]):
            outfile.write(line +', '+ x)
            break

I didn't do any formatting of each string, which you can do if you like. But because presumably the order isn't the same in both files, you either need to perform a match (which is easier if you read them into a real data structure - you could try csv if you'd like), or just loop over both lists assuming they're not too huge.

Sajjan Singh
  • 2,523
  • 2
  • 27
  • 34
1

Isn't your data.txt already in the format you want your match_list.txt to be in?

for line in match_data :
   for match in match_list :
      if match[:-1] in line : # careful of \n
         # write line to file
         break
ejrb
  • 338
  • 3
  • 9
  • it is, just the matches order is different. I needed it to be ordered as in the `match_list` file. – nutship Apr 12 '13 at 16:40
1

Excuse me. I see the "batch-file" tag in your question, so I posted a Batch file .bat solution as an alternative. My method consist in load first all the lines of data file into an array indexed by the team name; this way, when the match_list file be processed, the corresponding elements of data can be directly accessed. Here it is:

@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
Aacini
  • 65,180
  • 12
  • 72
  • 108