Good evening Everyone, I am David tzu a 9 years old, and I really need help with this problem about the alignment of the word properly, I tried the print only but it doesn't fit that much.
sample.txt consist of:
David Tzu January 13, 2012 FriedChicken
Mama March 15, 1980 Sphagetti
Papa June 27, 1981 Burger
here's my code:
file = open("sample.txt", "r")
line_count = 1
for line in file:
if line != "\n":
line_count = 1
file.close()
with open("sample.txt") as fi:
print("")
for x in range(1,line_count):
print(x, fi.readline().replace("\n", ""))
print("")
the current result of the code above is like this:
1 David Tzu January 13, 2012 FriedChicken
2 Mama March 15, 1980 Sphagetti
3 Papa June 27, 1981 Burger
Expected Result:
Name Birthday Favorite Food
1 David Tzu January 13, 2012 FriedChicken
2 Mama March 15, 1980 Sphagetti
3 Papa June 27, 1981 Burger
I am really sorry for my terrible english, Hope you all understand what I am trying to convey.
additional code: #Len of words
file = open("sample.txt", "r")
line_count = 1
for line in file:
if line != "\n":
line_count = 1
file.close()
with open("sample.txt") as fi:
print("")
for x in range(1,line_count):
print(x, fi.readline().replace("\n", ""))
print("")
file = open("Sample.txt", "rt")
data = file.readline()
words = data.split()
print(len(words))
CodePudding user response:
I don't know if I understand your problem.
with your example it seems you need only to add first row with correct number of spaces
print(' Name Birthday Favorite Food')
Minimal working example.
I use io
only to simulate file in memory so everyone can simply copy it and test it.
text = '''David Tzu January 13, 2012 FriedChicken
Mama March 15, 1980 Sphagetti
Papa June 27, 1981 Burger'''
import io
#fh = open("sample.txt")
fh = io.StringIO(text)
print(' Name Birthday Favorite Food')
line_count = 0
for line in fh:
line = line.strip()
if line:
line_count = 1
print(f"{line_count:3} {line}")
Result:
Name Birthday Favorite Food
1 David Tzu January 13, 2012 FriedChicken
2 Mama March 15, 1980 Sphagetti
3 Papa June 27, 1981 Burger
If you have more rows than 999 then you may need bigger number in {line_count:3}
and more spaces before Name
You have to only use fh = open()
, fh.write(line '\n')
, fh.close()
instead of print()
to have it in file. OR you can open use print(..., file=fh)
instead of write()
If you don't know how long are columns then you may need to read all lines, split to columns and then get len()
for every value in columns and get max(all_lenght_in_column)
for every column - and later you can use this value with string formatting
like {variable:lenght}
. But it may need regex
to split columns on 2 or more spaces
(\s{2,}
). So all this is much more complicated.
Eventually you may try to use pandas
and read_csv
with separator \s{2,}
text = '''David Tzu January 13, 2012 FriedChicken
Mama March 15, 1980 Sphagetti
Papa June 27, 1981 Burger'''
import pandas as pd
import io
#fh = "sample.txt"
fh = io.StringIO(text)
df = pd.read_csv(fh, names=['Name', 'Birthday', 'Favorite Food'], sep='\s{2,}', index_col=False)
df.index = 1
print(df)
Result:
Name Birthday Favorite Food
1 David Tzu January 13, 2012 FriedChicken
2 Mama March 15, 1980 Sphagetti
3 Papa June 27, 1981 Burger
And you can get it as df.to_string()
and save in normal text file.
with open('output.txt', 'w') as fh:
fh.write(df.to_string())
CodePudding user response:
You can do it this way if you know the space between the columns in file is a tab space.
with open("sample.txt") as ftr:
line_number = 1
print("\tName \tBirthday \tFavorite food")
for line in ftr.readlines():
print("%s \t%s" % (line_number, line))
line_number = 1