0

I want to use this text file in python, Read line by line, and want output as a list of lists with 9 columns (Team, Date, Rnd, A, W, D, L, Venue,Date)like:

Arsenal 2021 Rnd A W D L Venue Date R1 Tottenham 1 0 0 Emirates March R2 Man utd 0 1 0 Old Trafford March Total Average 1234 5678 Arsenal 2020 Rnd A W D L Venue Date R1 Chelsea 1 0 0 Stamford Bridge March R2 Mancity 0 1 0 Ethiad March Total Average 1234 5678 ..........and so on

|Arsenal|
| --- |
|2021|
| --- |
|Rnd|A|W|D|L|Venu|Date|
|R1|Tottenham|1|0|0|Emirates|March|
|R2|Man utd|0|1|0|Old Trafford|March|
|Total|Average|1234|5678|
| --- |
|Arsenal|
| --- |
|2020|
| --- |
|Rnd|A|W|D|L|Venu|Date|
|R1|Chelsea|1|0|0|Stamford Bridge|March|
|R2|Mancity|0|1|0|Ethiad|March|
|Total|Average|1234|5678|
| --- |
|Man Utd|
| --- |
|2021|
| --- |
|Rnd|A|W|D|L|Venu|Date|
|R1|Chelsea|1|0|0|Emirates|March|
|R2|Wolves|0|1|0|Old Trafford|March|
|Total|Average|1234|5678|
| --- |
|Man utd|
| --- |
|2020|
| --- |
|Rnd|A|W|D|L|Venu|Date|
|R1|Tottenham|1|0|0|Stamford Bridge|March|
|R2|Palace|0|1|0|Ethiad|March|
|Total|Average|1234|5678|


  • add a code snapshot please – cards Aug 09 '21 at 09:12
  • I can't quite make sense of what you're expecting. Could you check your output and its formatting? – Tranbi Aug 09 '21 at 09:16
  • Look at the `csv` module. You can specify `|` as the delimiter character. Every time your code gets a row with 3 elements you know you have a header. If the header `.isalpha()` then you have a team. If the header `.isdigit()` then you have a year. – BoarGules Aug 09 '21 at 09:19
  • Output ``Arsenal 2021 Rnd A W D L Venue Date``\n ``R1 Tottenham 1 0 0 Emirates March``\n ``R2 Man utd 0 1 0 Old Trafford March``\n ``Total Average 1234 5678``\n ``Arsenal 2020 Rnd A W D L Venue Date``\n ``R1 Chelsea 1 0 0 Stamford Bridge March``\n ``R2 Mancity 0 1 0 Ethiad March``\n ``Total Average 1234 5678 ..........and so on`` @ Tranbi, @cards, @BoarGules no pandas – user16603508 Aug 09 '21 at 09:41
  • add some python codes, which show your attempt... what about regular expression? – cards Aug 09 '21 at 11:00

1 Answers1

0

first_part = True
with open('Arsenal.txt', 'r') as fh:
    for line in fh:
        if first_part:
            word = line.rstrip('\n').rstrip('|')
            line = next(fh)
            line = next(fh)
            year = line.rstrip('\n').rstrip('|')
            line = next(fh)
            line = next(fh)
            first_part = False
        else:
            if line.startswith('|Total|'):
                first_part = True
            else:
                new_line = word + year + line
                print(new_line, end='') ````