欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python 刪除文件每一行的行號(hào)思路解讀

 更新時(shí)間:2021年11月06日 08:58:03   作者:(∪.∪ )...zzz  
有時(shí)候我們需要?jiǎng)h除代碼中的行號(hào),比如在把代碼復(fù)制到記事本中的時(shí)候,前邊的行號(hào)不刪除就沒辦法運(yùn)行,我們要手動(dòng)刪掉代碼段前的行號(hào),才能運(yùn)行代碼。如果有幾百行,就非常累,非常不爽,所以這種事還是要交給計(jì)算機(jī)去做

1. what

在這里插入圖片描述

這個(gè)行號(hào)真的很煩噶 試著寫一個(gè)py去掉

2. 思路

  • def second_of_str分割,取分隔符右邊的元素返回一個(gè)列表
  • def move傳入文件路徑,讀取每行,列表存儲(chǔ),調(diào)用 second_of_str分割后的存入新列表
  • 主函數(shù)調(diào)用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')

完成啦,成果~

在這里插入圖片描述

到此這篇關(guān)于Python 刪除文件每一行的行號(hào)思路解讀的文章就介紹到這了,更多相關(guān)Python 刪除文件行號(hào)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論