简体   繁体   中英

Adding a “-” sign in each line of a .txt file using Python

I have a text file with about 5 million lines and each line contains three numbers, like for example:

33.56 77.32 12.54

The numbers always have the same length.

Now I have to add a minus sign before the last number to get the following output:

33.56 77.32 -12.54

How can I do that in Python?

with open("filename.csv") as fh: 
    for line in fh: 
        line = line.strip()
        print(line[:12] + "-" + line[12:])

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