【python】去掉文件每一行的行号

这里写自定义目录标题

1. what

在这里插入图片描述
这个行号真的很烦噶 试着写一个py去掉

2. 思路

  • def second_of_str分割,取分隔符右边的元素返回一个列表
  • def move传入文件路径,读取每行,列表存储,调用 second_of_str分割后的存入新列表
  • 主函数调用move

3. 代码



def second_of_str(str,splitsymbol):
    s = str.split(splitsymbol,1)   # 分隔符右边的元素
    # if  len(s) == 1:
    #     return []
    return s[1]

def move(file_path, new_file_path):
    with open(file_path,encoding='utf-8') as f:
        content = f.read().splitlines()

    # for line in content:
    #     print(line)

    new_content = []
    for line in content:
        new_line = second_of_str(line,'.')
        # 去掉后的每行加入新列表
        new_content.append(new_line)

    for line in new_content:
        print(line)

    f1 = open(new_file_path,encoding='utf-8',mode = 'w')
    for line in new_content:
        f1.writelines(line)
        f1.writelines(["\n"])
    f1.close()
    return new_content

if __name__ == '__main__':
    f = move('1.txt','2.txt')

完成啦,成果~

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/grb819/article/details/121149513