简体   繁体   English

Python:删除特定字符(,)之后的所有内容

[英]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.我在一个文件中有数百万行(9GB)(无法通过 Notepad++ 打开),我想删除特定字符后的所有单词。 (,) (,)

Example of line :线路示例:

data1,data2,data3,data4数据1、数据2、数据3、数据4

I want to keep the (data1) and delete everything else.我想保留(data1)并删除其他所有内容。

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:您可以在每行上使用 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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