简体   繁体   中英

Python : Remove everything after a specific character (,)

I have millions of lines inside a file (9GB)(can't opened by Notepad++) and i want to remove all the words after a specific characters. (,)

Example of line :

data1,data2,data3,data4

I want to keep the (data1) and delete everything else.

Thanks in advance.

You can use split on each line to split the line on the first comma and only write out the left part of the split:

For example:

with open('input.txt') as infile:
    with open('output.txt','w') as outfile:
        for line in infile.readline():
            outfile.write(line.strip().split(",",1)[0]+"\n")

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