I have a data file (.txt
) containing some lines, each line as following:
0 45 1 31 2 54 3 54 4 64
With a white space before zero, two spaces between each two integers and a white space at the end. What I want is to have it like following:
0 45 1 31 2 54 3 54 4 64
I am trying everything (using Python) but I am not successful!
Of course at the end I want to reform it to:
45 31 54 54 64
That is eliminating the numbers 0 to 4 as well. But this last step is maybe easier to do if I reach the first one.
For example I have tried this:
with open('myfile', rt') as openfile, open('myfile_2, 'a') as csvfile:
for line in openfile:
A = str(line).replace(' ', ' ')
Writer = csv.writer(csvfile, delimiter=' ', quotechar=' ')
Writer.writerow([A])
But yet in the `myfile_2' the string is not corrected.
Made Changes Accordingly:
with open('newtes.txt', 'w') as outfile, open('tes.txt', 'r') as infile:
for line in infile:
outfile.write(line.replace(' ',' ').strip())
edit 1 : strip() added as suggested in the comment
edit 2 : Made Changes.