I have a text file with lots of numbers, one integer/float per line.
Blank lines with no data are indicated with a '-' and id like to remove the hyphen with code. But, while doing that, avoid removing the hyphen from negative numbers (-4 etc).
Is this possible to begin with?
I have tried using str.replace
and str.split
with no success as it either removes all the hyphens or none.
this will remove all trailing -
from the line. If the line contained spaces you can change num.rstrip("-")
to num.strip().rstrip("-")
with open("your_file") as fp:
file_content = [num for num in fp if num.rstrip("-")]
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.