简体   繁体   中英

string '-1' can't be converted to float

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.

You missed line assignment

if '-' in line:
    line = line.replace('-','-')
    print('replaced') 

I just looked at your code: it does replace('-','-') – which is the same character.

You should either do replace('–','-') , or, for better clarity of what you do, replace(u'\–', '-') .

Besides, your re-assignment to line is missing.

use both answers your code should be:

with open('text.txt', encoding='utf8') as fobj:
        all_ = []
        for line in fobj:
            line = line.strip()
            if  in line:
                 line.replace(, '-')
                print('replaced', line)
            all_.append(line)
    with open('text2.txt','w',encoding='utf8') as f:
        for i in all_:
            print(i)
            f.write(i)
            f.write('\n')

result is

replaced / 11/3 / 2 / 0 / 0/-1 /
replaced / 12/4 / 4 / -1 / 0/-1 / H
replaced / 12/5 / 1 / 0 / 0/-1 / H
/ 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM