侧边栏

利用python脚本删除txt文件每行后4个字符,并换行

利用python脚本删除txt文件每行后4个字符,并换行

# import os
filename = r"123.txt"
new_filename = r"1234.txt"
with open(filename,encoding="utf-8") as f1, open(new_filename,"w",encoding="utf-8") as f2:
    for line in f1:
        new_line = line[:-4]
        f2.write(new_line)
        f2.write('\n')
f1.close()
f2.close()
# import os
filename = r"123.txt"
new_filename = r"12345.txt"
with open(filename,encoding="utf-8") as f1, open(new_filename,"w",encoding="utf-8") as f2:
    for line in f1:
        new_line = line[3:]
        f2.write(new_line)
f1.close()
f2.close()

有时我们并不想让转义字符生效,我们只想显示字符串原来的意思,这就要用r和R来定义原始字符串。如:print r'\t\r'

  实际输出为“\t\r”。

常见的转义字符

转义字符 输出
\'   '

\"

"
\a   ‘bi’响一声
\b 退格
\f  换页(在打印时)
\n 回车,光标在下一行
\r 换行,光标在上一行
\t 八个空格
\\ \

posted @ 2022-03-08 17:32  菜鸟-传奇  阅读(897)  评论(0编辑  收藏  举报