Append float data at the end of each line in a text file

2024/11/18 16:40:08

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 
Answer

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 countercounter += 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.

https://en.xdnf.cn/q/120052.html

Related Q&A

Change and Sort list in list in specific order in python

Hello guys I have this type of data in list in list array = [ ["PRODUCT NAME PACK","BAIGAM KOT","FIAZ BAGH","OLD ANARKALI","SULTAN PURA","TEZAB AA…

How to interact with the reCAPTCHA Solve the challenge button using Selenium and Python

Im trying to interact with the recaptcha Solve the challenge button on image verification popup using Selenium and Python. The xpath looks correct in dev tools but using Selenium unable to interact wit…

How to convert an adjacency matrix to an adjacency list with python?

I have an adjacency matrix like: [[ 0., 15., 0., 7., 10., 0.],[ 15., 0., 9., 11., 0., 9.],[ 0., 9., 0., 0., 12., 7.],[ 7., 11., 0., 0., 8., 14.],[ 10., 0., 12., …

how to plot a box plot of a column of a data frame in two groups in matplotlib

I have the following data frame:value total_spend margin1 29493.14 10203.371 27551.22 19003.072 22881.26 15142.681 15254.77 12337.221 11873.95 9424.231 11382.89 8767.83…

Text manipulation to form an equation

a=0.77 ,b=0.2 ,c=0.20, d=0.79 ,z=(c+d), e=(z*a) ,output=(z+e) I have a text file like above. I need a parser logic that will throw an equation like output=(0.20+0.79)+((0.20+0.79)*a) what are some effi…

Python If statement and logical operator issue [duplicate]

This question already has answers here:How to test multiple variables for equality against a single value?(31 answers)Closed 6 years ago.Im trying to create a small python program to emulate rolling a…

How do I replace a specific string in a 2d array?

Im making a program that Identifies if a blank tile exists or not. I already have a code in my 2d array which is arr2 = [[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0][0 0 0 0 0 0 0 0 0 0 0 0 0 0 0][0 0 0 0 0 0 0 0 0…

from _dlib_pybind11 import * ModuleNotFoundError: No module named _dlib_pybind11

I actually working on a face recognition project but getting an error such as: from _dlib_pybind11 import * ModuleNotFoundError: No module named _dlib_pybind11Please help Ill appreciate any bits of hel…

Sudoku with user input

I am trying to write a Sudoku game with user input. So the user can choose what row/column it wants to and what number. I have to import it from a text file and have made a save and load function. I ha…

Move user to a textchannel as soon as he has pressed a button

I have a problem. I would like to move a user from one specific textchannel to another specific textchannel as soon as he has pressed a button. Unfortunately I get an error. class MyView(discord.ui.Vie…