python - 删除python文件中每一行逗号前的所有字符

标签 python file io

我需要一个 python 中的小函数,它可以读取一个文件,然后删除所有字符,包括一个逗号字符。例如以下两行文件:

hello,my name is
john,john, mary

会是:

my name is
john, mary

最佳答案

已建议您使用 re.split();但是,str 的常规 split() 方法也应该足够了:

with open('new_file', 'w') as f_out, open('my_file') as f_in:
    for line in f_in:
        new_str = ','.join(line.split(',')[1:])
        f_out.write(new_str)

关于python - 删除python文件中每一行逗号前的所有字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9639966/

相关文章:

perl - 我可以在Perl中打开的文件句柄数量是否有限制?

扩展 html python Flask 中无法识别 JavaScript onload

Python Keras - Windows 和 Linux 之间的兼容性

python - 使用 Zeep 向 SOAP 服务发送原始 XML 请求(尝试复制参数)

php - 将txt文件读入二维数组

c - 获取文本文件的每个元素,直到“:”并存储在struct变量中

python - SCAPY发送数据包错误OSError : [Errno 97] Address family not supported by protocol

windows - 在 Windows 上,如何检测文件的行尾?

io - io_uring 到底是什么?

java - 将 java System.out.println 重定向到 jruby 中的文件