'python how to delete a line from a txt file and add data
my program reads all txt files from a folder and convert the numbers in the txt files. one of my txt file for example is this
1 0.487500 0.751667 0.112500 0.246667
0 0.464375 0.648333 0.046250 0.083333
4 0.500000 0.352500 0.055000 0.098333
they all have the same frame like above but the rows could have less or more. so far i've been able to convert them using below code
import os
for root, dirs, files in os.walk('./'):
for idx, file in enumerate(files):
fname, ext = os.path.splitext(file)
if ext in ['.txt']:
with open((file),'r') as f:
lines = f.readlines()
for line in lines:
item = line.split(" ")
item[1] = float(item[1])
item[1] = ((item[1]) * 800 - 256) / 288
item[1] = '{:.6f}'.format(round(item[1],6))
item[1] = str(item[1])
item[2] = float(item[2])
item[2] = ((item[2]) * 600 - 88) / 512
item[2] = '{:.6f}'.format(round(item[2],6))
item[2] = str(item[2])
item[3] = float(item[3])
item[3] = ((item[3]) * 800) / 288
item[3] = '{:.6f}'.format(round(item[3],6))
item[3] = str(item[3])
item[4] = float(item[4])
item[4] = ((item[4]) * 600) / 512
item[4] = '{:.6f}'.format(round(item[4],6))
item[4] = str(item[4])
item = ' '.join(item)
print (item)
and result using the above code and the txt file is
1 0.465278 0.708985 0.312500 0.289063
0 0.401042 0.587890 0.128472 0.097656
4 0.500000 0.241211 0.152778 0.115234
which works great but i need to replace the results to the original. how would i be able to do it?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
