-2

I have the following table in a txt file:

       |Client | Container weight | Country of Origin | Product code |
       |S4378  |     450 Ton      | China             | 457841       |
       |       |     350 Ton      | Japan             | 457841       |
       |       |     900 Ton      | Japan             | 457841       |
       |       |     589 Ton      | China             | 457841       |
       |S4978  |    1400 Ton      | Mexico            | 457841       |
       |       |     980 Ton      | Emirates          | 457841       |
       |       |     550 Ton      | China             | 457841       |
       |S4578  |     450 Ton      | China             | 457841       |

The table starts in Line 81.

I want to create an output text file as follows:

       |Client | Container weight | Country of Origin | Product code |
       |S4378  |     450 Ton      | China             | 457841       |
       |S4378  |     350 Ton      | Japan             | 457841       |
       |S4378  |     900 Ton      | Japan             | 457841       |
       |S4378  |     589 Ton      | China             | 457841       |
       |S4978  |    1400 Ton      | Mexico            | 457841       |
       |S4978  |     980 Ton      | Emirates          | 457841       |
       |S4978  |     550 Ton      | China             | 457841       |
       |S4578  |     450 Ton      | China             | 457841       |

I have tried extracting lines:

file = open('containers.txt')
lines_to_print = [80, 81, 82, 83, 84, 85...]
for index, line in enumerate(file):
    if index in lines_to_print:
        print(line)

My question is how do I write this code, if my table has 300 rows?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Mimi
  • 1
  • `if index in range(80, 300)`? You shouldn't write out the lines anyway. And if you know that the table starts at line 80 and goes until the end, just [skip the first 80 lines](https://stackoverflow.com/questions/9578580/skip-first-couple-of-lines-while-reading-lines-in-python-file) and then loop until the end. No condition needed... – Tomerikoo May 23 '22 at 14:25
  • or `if index >= 80:` or `if 80 <= index < 300:` – furas May 27 '22 at 02:29

3 Answers3

0

My first suggestion would be to use a csv file, instead of this file. Is that possible for your situation? Python has a ton of libraries for parsing csv files. It's harder to parse a file when the file doesn't have a distinct character separating the different fields.

Did you export this table file from a program like SSMS? If so, they usually have support for exporting as csv instead.

Danimal
  • 68
  • 3
0

To print the lines after 80 is quite easy:

with open('containers.txt') as in_file:
    lines = in_files.readlines()
    if len(lines) > 80:
        for line in lines[80:]:
            print(line.strip())
  • The condition is not necessary. If there are not enough lines, `lines[80:]` will give an empty list and the loop will do nothing anyway... – Tomerikoo May 23 '22 at 14:26
0

When working with csv files, you should be using the pandas library. However, if you want to do it in plain python, line by line, this is one solution.

with open('containers.txt', 'r') as f:
    with open('output.txt', 'w') as out:
        for line in f:
            if line: #skip empty lines
                data = list(filter(None, map(lambda x: x.strip(), line.split('|')))) #one-liner to split the line in its data-fields and remove empty ones
                if len(data) == 4: #if the Client is missing the len will be 3
                    client = line[:9] #save the Client field
                    out.write(line + '\n')
                else:
                    out.write(f'{client}{line[9:]}\n') #use the saved Client field to fill in the gaps

The output file now contains:

|Client | Container weight | Country of Origin | Product code |
|S4378  |     450 Ton      | China             | 457841       |
|S4378  |     350 Ton      | Japan             | 457841       |
|S4378  |     900 Ton      | Japan             | 457841       |
|S4378  |     589 Ton      | China             | 457841       |
|S4978  |    1400 Ton      | Mexico            | 457841       |
|S4978  |     980 Ton      | Emirates          | 457841       |
|S4978  |     550 Ton      | China             | 457841       |
|S4578  |     450 Ton      | China             | 457841       |
alec_djinn
  • 10,104
  • 8
  • 46
  • 71
  • The file is given to me in txt files and the output has to be a new txt file. I tried pandas, did not know how to fill the empty spaces. – Mimi May 23 '22 at 16:35
  • @Mimi updated to write a file instead of printing. If you can share the txt file, I can add the pandas' code. – alec_djinn May 24 '22 at 08:09