-2

I have a .txt file and I'm trying to add float number at the end of each line with incremented value than the last line. This is what I have in the .txt file:

>520.980000 172.900000 357.440000
>320.980000 192.900000 357.441000
>325.980000 172.900000 87.440000

and I'm trying to have this result:

>520.980000 172.900000 357.440000 1.1
>320.980000 192.900000 357.441000 1.2
>325.980000 172.900000 87.440000 1.3

But can't seem to figure out how to iterate and increment the data.

EDIT:

as @Daweo recommended I put my comment in the edit section due to formatting problem in the comment section. So I have a sequence of frames as below and I want to give for each group of sequence a number and incresing it for the following, for exemple: the group with the 0 index (the first column)I will add 1 at the end of each line, the next with index 1 I will add 2 at the end of each line and so on:

0 0 0 0 -1.793451 296.744956 161.752147 455.226042 292.372804 
0 1 0 0 -1.936993 737.619499 161.531951 931.112229 374.000000
0 2 0 0 -2.523309 1106.137292 166.576807 1204.470628 323.876144 
1 -1 -1 -1 -10.000000 228.120000 183.030000 258.830000 217.340000
1 -1 -1 -1 -10.000000 59.210000 191.300000 137.370000 227.430000 
1 0 0 0 -1.796862 294.898777 156.024256 452.199718 284.621269 2.000000 
1 1 0 0 -1.935205 745.017137 156.393157 938.839722 374.000000 1.739063 
1 2 0 0 -2.530402 1138.342096 160.872449 1223.338201 324.146788
2 -1 -1 -1 -10.000000 236.270000 175.500000 267.210000 211.030000
2 -1 -1 -1 -10.000000 68.906000 183.810000 145.870000 224.020000 
2 0 0 0 -1.800343 293.093560 150.470149 449.259225 277.104290 2.000000 
onemanarmy
  • 51
  • 8
  • 1
    Could you show us what you have tried yet ? Do you know how to read the first file, how to write the second one (except the increment part), could you give pieces of code? – Corentin Limier Jun 20 '19 at 14:45
  • What's the starting point of that float value? How much do you want to increase in each iteration? – Mezbaul Haque Jun 20 '19 at 14:45
  • @CorentinLimier Im a bit new to python so I was embarressed to post my code, but this is what I tried so far, but I get stuck at parsing the data and adding the value filepath = "0004.txt" with open(filepath) as fp: lines = fp.read().splitlines() with open(filepath, "w") as fp: for line in lines: print(line + "#", file=fp) – onemanarmy Jun 20 '19 at 14:55
  • @Mezbaメ my starting point is 1 and I want to increase by 0.1 – onemanarmy Jun 20 '19 at 14:57
  • @OneManArmy don't be embarrassed, we all started at the same level. – Corentin Limier Jun 20 '19 at 15:04
  • Possible duplicate of [How to read a file line-by-line into a list?](https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list) –  Jun 20 '19 at 15:04

2 Answers2

1

I would do it following way: Assume that you have file input.txt:

520.980000 172.900000 357.440000
320.980000 192.900000 357.441000
325.980000 172.900000 87.440000

then:

from decimal import Decimal
import re
counter = Decimal('1.0')

def get_number(_):
    global counter
    counter += Decimal('0.1')
    return " "+str(counter)+'\n'

with open("input.txt","r") as f:
    data = f.read()

out = re.sub('\n',get_number,data)

with open("output.txt","w") as f:
    f.write(out)

After that output.txt is:

520.980000 172.900000 357.440000 1.1
320.980000 192.900000 357.441000 1.2
325.980000 172.900000 87.440000 1.3

Note that I used Decimal to prevent problems with float (like something.999999...) appearing. I used regular expression (re) to find newlines (\n) and replace it with subsequent numbers by passing function as 2nd re.sub argument.

Daweo
  • 31,313
  • 3
  • 12
  • 25
  • Thank you this worked! let me put my two cents and say that I changed the output name to be the same as the input so that I don't have to rename 300+ files when I apply this fucntion – onemanarmy Jun 20 '19 at 15:24
  • since your method worked, let me ask you if you can tell me how to apply this on multiple file, the situation is that a have a folder with 20 files and I want to apply this to all of them – onemanarmy Jun 20 '19 at 15:33
  • 1
    @OneManArmy: take look at `os.listdir` function, it accepts one argument - path to catalog and `return`s `list` of its content. – Daweo Jun 20 '19 at 16:50
  • thank you for your response, actually I was wonder if you can help with a bit complex issue ragarding the txt file appending. so i have a sequence of frames as below in the next comment and I want to give for each group of sequence a number and incresing it for the following, for exemple: the group with the 0 index (the first column) will add 1 at the end of each line, the next with index 1 I will add 2 at the end of each line and so on. – onemanarmy Jun 21 '19 at 14:35
  • `0 -1 -1 -1 -10.000000 219.310000` `0 -1 -1 -1 -10.000000 47.560000` `0 0 0 0 -1.793451 296.744956` `0 1 0 0 -1.936993 737.619499` `0 2 0 0 -2.523309 1106.137292` `1 -1 -1 -1 -10.000000 228.120000` `1 -1 -1 -1 -10.000000 59.210000` `1 0 0 0 -1.796862 294.898777` `1 1 0 0 -1.935205 745.017137` `1 2 0 0 -2.530402 1138.342096` `2 -1 -1 -1 -10.000000 236.270000` `2 -1 -1 -1 -10.000000 68.906000` `2 0 0 0 -1.800343 293.093560` `2 1 0 0 -1.933364 752.406083` `2 2 0 0 -2.538744 1151.358043` – onemanarmy Jun 21 '19 at 14:49
  • Maybe, but due to formatting of your example data (in comment) I am not able to grasp what you wants exactly. Please edit your original post or consider posting new question. – Daweo Jun 21 '19 at 17:29
0

Without other modules (not that you should avoid them!):

with open("numbers.txt", "rt") as fin:
    with open("numbers_out.txt", "wt") as fout:
        counter = 1.0
        for line in fin:
            counter += 0.1
            fout.write(f"{line.rstrip()} {counter:.1f}\n")

numbers_out.txt:

>520.980000 172.900000 357.440000 1.1
>320.980000 192.900000 357.441000 1.2
>325.980000 172.900000 87.440000 1.3

GordonAitchJay
  • 4,640
  • 1
  • 14
  • 16
  • Well thank you for your effort. But as I did like you showed in your response, the whole content of the file got earased and I have an empty file, any idea what might cause that? Thank you – onemanarmy Jun 20 '19 at 15:03
  • That's very strange. Inexplicable! Which file? numbers_out.txt will be overwritten each time you run the above code. – GordonAitchJay Jun 20 '19 at 15:05
  • 1
    @OneManArmy The "w" flag will truncate a file before writing to it. I also don't recommend nesting `with` environments, it's not clean nor necessary. –  Jun 20 '19 at 15:08
  • It appears you used the input file as your output file! You can do that, but you'll have to read in the whole input file before writing to it, which shouldn't be an issue. I hope you didn't lose that data for good. – GordonAitchJay Jun 20 '19 at 15:23