簡體   English   中英

wc -l和python行數不同

[英]wc -l and python line count differ

我不知道為什么使用bash簡單的行數是給了我不同的行數比來計算使用python給定文件(版本3.6) 這里train_en.txt )和這里train_de.txt )。 在bash中,我正在使用命令:

wc -l train_en.txt
wc -l train_de.txt

輸出分別為4520620和4520620。

在python中,我正在使用命令:

print(sum(1 for line in open('train_en.txt')))
print(sum(1 for line in open('train_de.txt')))

輸出分別為4521327和4521186。

當我使用python命令

len(open('train_en.txt').read().splitlines())
len(open('train_de.txt').read().splitlines())

我分別得到4521334和4521186( train_en.txt結果與之前的python命令不匹配)。

作為參考,這些是通過將通用爬網Europarl新聞評論數據集( 按此順序)從WMT '14英語到德語翻譯任務連接而產生的平行語料庫,並且應該具有相同數量的行。

\\n s可以被視為多字節字符而不是實際\\n 可以通過使用字節串編碼來避免這種情況。 命令

print(sum(1 for line in open('train_en.txt', mode='rb')))
print(sum(1 for line in open('train_de.txt', mode='rb')))
len(open('train_en.txt', mode='rb').read().splitlines())
len(open('train_de.txt', mode='rb').read().splitlines())

所有結果都是4520620(匹配wc -l的輸出),這意味着英語和德語語料庫按需要並行。

感謝@CharlesDuffy的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM