I tried to read from every line until the ,
so that I can edit it with format to add my text but I don't know which code to use to read until a certain point for every line. I tried to search on the web but I couldn't find anything
def pretty_print():
file = open('kaartnummers', 'r')
# print(file.read())
print(file.read())
pretty_print()
Contents of kaartnummer
text file
325255, Jan Jansen
334343, Erik Materus
235434, Ali Ahson
645345, Eva Versteeg
534545, Jan de Wilde
345355, Henk de Vries
Desired output:
Jan Jansen heeft kaartnummer: 325255
Erik Materus heeft kaartnummer: 334343
Ali Ahson heeft kaartnummer: 235434
Eva Versteeg heeft kaartnummer: 645345
Jan de Wilde heeft kaartnummer: 534545
Henk de Vrie heeft kaartnummer: 345355
with open("sample.txt", "r") as f:
for line in content_list:
if "," in line:
line_sepperated = line.split(",")
name = line_sepperated[1]
num = line_sepperated[0]
print(f'{name} heeft kaartnummer: {num}')
else:
print(line)
short explanation:
,
exist - split the line by it and print by formatting wanted, if not - print the whole linei tried it one more time but i am now struggling with one last thing i tried it in many ways if fprint with str(num) + '' and even . format but my result is still not what i want.
def pretty_print():
line = open('kaartnummers', 'r')
with open("kaartnummers", "r") as f:
for line in line:
if "," in line:
line_sepperated = line.split(",")
name = line_sepperated[1]
num = line_sepperated[0]
# print(f'{name} heeft kaartnummer: {num}')
# print('{} heeft kaartnummer: {}' .format(*[name], *[num]))
print(str(name)+' heeft kaartnummer: '+str(num))
else:
print(line)
pretty_print()
because if i print that code it will give this as a result
https://i.stack.imgur.com/7d7fy.png
Jan Jansen
heeft kaartnummer: 325255
Erik Materus
heeft kaartnummer: 334343
Ali Ahson
heeft kaartnummer: 235434
Eva Versteeg
heeft kaartnummer: 645345
Jan de Wilde
heeft kaartnummer: 534545
Henk de Vries heeft kaartnummer: 345355
i fixed it.
the command that fixed it was
.replace("\n", "")
right now i have
print(f'{num} heeft kaartnummer: {name}'.replace("\n", ""))
print('{} heeft kaarnummer: {}'.format(*[num], *[name]).replace("\n", ""))
print(str(num).replace("\n", "")+' heeft kaartnummer: '+str(name).replace("\n", ""))
and it works on all the 3 print lines
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.