How do I remove extra spaces between my conclusions?
Here is the first code that printed my output in order:
import csv
import random
import os.path
def filename ():
filename = 'filename.txt'
with open ("filename.txt", "r") as data_file:
for line in data_file.readlines():
line = line.strip().split()
print (line [0], line [1], line[2])
This was the result:
1 63075384 0.781952678
1 212549126 0.050216027
2 35003118 0.027205438
2 230357107 0.065453827
3 77023025 0.098224352
3 225622058 0.785312636
Then I wanted to randomize my conclusion. So here is the second code where I did it:
import random
with open('filename.txt') as fin:
lines = list(fin)
random.shuffle(lines)
for line in lines:
print (line)
Here is the result. Which adds extra white 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:
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 I change the second code shown above so that I can print my output without any extra spaces?
I know this may seem like a duplicate question, but the fact is that I have searched the internet and I cannot find a solution to this problem. I'm happy to clarify everything. Thank.
source to share
Try the following:
import random
with open('filename.txt') as fin:
lines = list(fin)
random.shuffle(lines)
for line in lines:
print(line, end='')
Setting end=''
in print()
avoiding printing a new line.
For more information see the Python docs atprint()
.
source to share