How to print a table from a text file and fill empty spaces?

2024/7/5 11:51:57

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?

Answer

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.

https://en.xdnf.cn/q/120091.html

Related Q&A

Open a file in python from 2 directory back

I want to read a file from 2 folders back.. with open(../../test.txt, r) as file:lines = file.readlines()file.close()I want to read from ../../ two folders back. but not work.. How i can do that ?

Do string representations of dictionaries have order in Python 3.4?

I know dictionaries themselves in Python do not have order. However, Im rather curious if when you call str() on a dictionary if it is always in the same order. It appears to be sorted (by key), no mat…

BeautifulSoup Scraping Results not showing

I am playing around with BeautifulSoup to scrape data from websites. So I decided to scrape empireonlines website for 100 greatest movies of all time. Heres the link to the webpage: https://www.empireo…

How to verify username and password from CSV file in Python?

I am doing a Python project where I have to verify my username and password from a csv file where the first two rows and columns have the username and password as hi.Current Code: answer = input("…

adding validation to answer in quiz gives wrong answers

I am a complete novice with Python and working on a multiple choice quiz that reads questions from a file and keeps a score that then writes to a file. Everything was working perfectly until I added v…

Why do I get None as the output from a print statement? [duplicate]

This question already has answers here:Why is "None" printed after my functions output?(7 answers)Closed 2 years ago.def print_name(name):print(name)print(print_name(Annabel Lee))Why do I ge…

How to collect tweets about an event that are posted on specific date using python?

I wish to collect all tweets containing specific keywords(ex:manchesterattack,manchester) that are posted about the manchester attack from 22may. Can anyone provide me a code to collect tweets using py…

Pivoting a One-Hot-Encode Dataframe

I have a pandas dataframe that looks like this:genres.head()Drama Comedy Action Crime Romance Thriller Adventure Horror Mystery Fantasy ... History Music War Documentary Sport Musical W…

How to declare multiple similar variables in python? [duplicate]

This question already has answers here:How do I create variable variables?(18 answers)Closed 5 years ago.How can I declare multiple (about 50) variables that count from slider1 to slider50 ? Is there…

what does means this error broken pipe? [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:TCP client-server SIGPIPE I would like know what does this error mean?