python - How can I remove the extra spaces between my output? -
here first code printed output in order:
import csv import random import os.path def filename (): filename = 'filename.txt' open ("filename.txt", "r") data_file: line in data_file.readlines(): line = line.strip().split() print (line [0], line [1], line[2])
this output:
1 63075384 0.781952678 1 212549126 0.050216027 2 35003118 0.027205438 2 230357107 0.065453827 3 77023025 0.098224352 3 225622058 0.785312636
i wanted randomize output. here second code did that:
import random open('filename.txt') fin: lines = list(fin) random.shuffle(lines) line in lines: print (line)
here output. adds blank space between each line:
2 35003118 0.027205438 3 77023025 0.098224352 2 230357107 0.065453827 1 212549126 0.050216027 3 225622058 0.785312636 1 63075384 0.781952678
my desired output is:
2 35003118 0.027205438 3 77023025 0.098224352 2 230357107 0.065453827 1 212549126 0.050216027 3 225622058 0.785312636 1 63075384 0.781952678
how can alter second code shown above can print output without blank spaces?
i know may seem duplicate question fact have searched online , can not seem find solution problem. happy clarify anything. thank you.
your lines contain newline character @ end of them, in loop:
for line in lines: print (line)
it print newline in line
string. strip line
remove newline:
for line in lines: print (line.strip())
Comments
Post a Comment