I try to read large sets of numbers from a text file opened with utf-8 encoding.
The text file was a copy/paste from a pdf.
The problem lies in the negative numbers (-1, -2 etc):
I stripped everything, so the individual string bits look like this: -1, -2 etc.
Then I want to calculate with them and convert them with float(), but I get an error:
can't convert string to float: '-1'
I concluded, the '-' could be interpreted as a long '-', whatever that is called and replaced it manually in the text file by a '-'. Now it worked for this single string, float() converted it.
I wrote a small script that finds and replaces all '-' by '-' in the text file, but that didn't work.
with open('text.txt', encoding='utf8') as fobj:
all = []
for line in fobj:
line = line.strip()
if '-' in line:
line.replace('-','-')
print('replaced')
all.append(line)
with open('text2.txt','w',encoding='utf8') as f:
for i in all:
print(i)
f.write(i)
f.write('\n')
Why is it I can replace '-' by '-' manually but not with this script?
Thanks for help.
Example snipped from the text file:
/ 11/3 / 2 / 0 / 0/–1 /
/ 11/5 / 0 / 2 / 0/0 / N
/ 12/3 / 1 / 0 / 0/0 /
/ 12/4 / 1 / 1 / 0/0 / NS
/ 12/4 / 4 / –1 / 0/–1 / H
/ 12/5 / 1 / 0 / 0/–1 / H
/ 12/5 / 2 / 0 / 0/-1 / H
/ 11/4 / 0 / 0 / 0/0 / H
You can actually see the difference between the second last and third last lines -1. In this copy that is. i replaced the last - manually.