Home > Enterprise >  Subtracting 1 from the first number of a txt file using python
Subtracting 1 from the first number of a txt file using python

Time:08-24

I have been stuck in this problem for hours now. I have a txt file where the following is the contents.

10 0.4600 0.4833 0.0433 0.0833
4 0.7833 0.6350 0.1167 0.0933
7 0.3583 0.4933 0.1667 0.1700

I'm trying to subtract 1 on the first numbers of the content to achieve the following result while maintaining the floating point numbers:

9 0.4600 0.4833 0.0433 0.0833
3 0.7833 0.6350 0.1167 0.0933
6 0.3583 0.4933 0.1667 0.1700

The following is my attempt to solve the problem but the whole content just disappears:

path = 'file.txt'
with open(path, "r ") as f:
  lines = f.readlines()
lines = str(int(lines[0].split()[0]) - 1)
with open(path, 'w') as file:
  file.writelines(lines)

I really need help for this problem. I've been trying for hours. Thanks in advance

CodePudding user response:

lines = list()

path = 'file.txt'
with open(path, "r ") as f:
    for line in f.readlines():
        split_line = line.strip("\n").split(" ")  # split on space character (and remove newline characters as well)
        lines.append(split_line)  # add split list into list of lines
  
  
for line in lines:
    line[0] = int(line[0]) - 1  # only subtract one from first element

with open(path, 'w') as file:  # rewrite to file

    for line in lines:
        # build the string to write
        write_me = ""
        for element in line:
            write_me  = f"{element} "
        file.write(write_me   "\n")

One thing to note is there will be trailing space and newline characters. If this is unwanted, let me know and I'll update this answer.

CodePudding user response:

your solution is no doubt a very accurate one. I just would like to make a few improvements:

lines = list()

path = 'file.txt'
with open(path, "r ") as f:
    for line in f.read().splitlines():
        # print(line)
        split_line = line.split(" ")  # split on space character (and remove newline characters as well)
        split_line[0] = str(
            int(split_line[0]) - 1)  # update the value inside the loop. the loop used in later not needed.
        lines.append(split_line)  # add split list into list of lines

# for line in lines:
#     line[0] = int(line[0]) - 1  # only subtract one from first element

with open(path, 'w') as file:  # rewrite to file
    for line in lines:
        # build the string to write
        # write_me = ""
        # for element in line:
        #     write_me  = f"{element} " # Another loop is not needed.
        write_me = ' '.join(line)  # Use join method to add the element together
        file.write(write_me   "\n")

Please compare yours code with mine and check the improvements.

Again, your code is absolutely correct just had a few scope of improvement. Please ignore my answer if you feel any other way.

Thanks!

CodePudding user response:

this would be cleaner in terms of lines of code:

import pandas as pd

f = 'G:\\path_to_file\\test.txt'
df = pd.read_csv(f, sep=" ", header=None)

df[0] = df[0]-1
print(df)

the result:

    0   1       2       3       4
0   9   0.4600  0.4833  0.0433  0.0833
1   3   0.7833  0.6350  0.1167  0.0933
2   6   0.3583  0.4933  0.1667  0.1700
  • Related