Dear ALL,
I have a similar problem with a string/replacement

132435 321350 35465162 6561516
546516 654613 65462133 6584652

and so on for some hundreds (!!) raws.

What is the best way to edit this textfile to have something like this?
132435,-321350,35465162,-6561516
546516,-654613,65462133,-6584652

Basically, I have to change the 'double space' in comma and put a minus in front of the second and forth block of numbers every raw.

Thanks a lot for any help or suggestion

Recommended Answers

All 8 Replies

Here idea of way:

>>> data = """132435 321350 35465162 6561516
546516 654613 65462133 6584652""".splitlines()
>>> for line in data:
	line = [(-1)**ind*int(number) for ind, number in enumerate(line.split())]
	print line

And is there a way to apply it to a file with 4 group of numbers every raw?

Oh, and what if numbers are not integer?
1529.03 1090.41 1551.12 350.998
1292.89 1130.88 1321.76 393.807
please note that there is a double space between every group.
Thanks again

Nobody helps me?
Even if I change the int to float, I still have problems, because now results with 10 zeros. I did a round(number,2) but it doesn't change that much. Any help?

Nobody helps me?
Even if I change the int to float, I still have problems, because now results with 10 zeros. I did a round(number,2) but it doesn't change that much. Any help?

Can you post your code and attach your file and describe the expected result precisely ?

OK, so in a text file I have a sequence of lines like the following
1529.03 1090.41 1551.12 350.998
1292.89 1130.88 1321.76 393.807
1232.2 896.872 1261.19 167.387
1711.87 1000.48 1730.33 275.271
1705.36 1207.87 1725.13 476.985
362.73 1293.13 412.651 564.386
418.009 1156.54 465.769 429.217

Now, the second and the forth number has to become negative and instead of the double space between numbers I need a comma.
Like this

1529.03,-1090.41,1551.12,-350.998
1292.89,-1130.88,1321.76,-393.807

The code that pyTony kindly suggested is (with my small editing)

aFile="numberlist.txt"

f = open(aFile)
for line in f:
 lines=line.splitlines()
 line = [round((-1)**ind*float(number),3) for ind,number in enumerate(line.split())]
 
 print line
f.close()

and it produces

[1529.03, -1090.4100000000001, 1551.1199999999999, -351.0]
[1292.8900000000001, -1130.8800000000001, 1321.76, -393.81]

how can I fix the decimals? and how can i update the file (or create a new one) with the new values?
Thanks

Here is keep it strings solution:

data = """1529.03 1090.41 1551.12 350.998
1292.89 1130.88 1321.76 393.807
1232.2 896.872 1261.19 167.387
1711.87 1000.48 1730.33 275.271
1705.36 1207.87 1725.13 476.985
362.73 1293.13 412.651 564.386
418.009 1156.54 465.769 429.217""".splitlines()

for line in data:
    print(''.join(number+sep for number, sep in zip(line.split(), (', -',', ',', -',''))))

OK great, now it's perfect (I manage also to export the result to another file)!
As I always do, I will now "dismantle" your code to understand how it works.
Thanks a lot,
Gianluca

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.