Windows下使用csv.writer寫檔案出現空行【已解決】
阿新 • • 發佈:2019-01-14
在學習pytorch教程中Chatbot Tutorial,跟著作者的程式碼復現,遇到了這個問題:在把格式化的對話資料寫入檔案時,發現多了空行,作者的程式碼是:
# Write new csv file
print("\nWriting newly formatted file...")
with open(datafile, 'w', encoding='utf-8') as outputfile:
writer = csv.writer(outputfile, delimiter=delimiter)
for pair in extractSentencePairs( conversations):
writer.writerow(pair)
輸出為:
Sample lines from file: b"Can we make this quick? Roxanne Korrine and Andrew Barrett are having an incredibly horrendous public break- up on the quad. Again.\tWell, I thought we'd start with pronunciation, if that's okay with you.\r\r\n" b"Well, I thought we'd start with pronunciation, if that's okay with you.\tNot the hacking and gagging and spitting part. Please.\r\r\n" b"Not the hacking and gagging and spitting part. Please.\tOkay... then how 'bout we try out some French cuisine. Saturday? Night?\r\r\n" b"You're asking me out. That's so cute. What's your name again?\tForget it.\r\r\n" b"No, no, it's my fault -- we didn't have a proper introduction ---\tCameron.\r\r\n" b"Cameron.\tThe thing is, Cameron -- I'm at the mercy of a particularly hideous breed of loser. My sister. I can't date until she does.\r\r\n" b"The thing is, Cameron -- I'm at the mercy of a particularly hideous breed of loser. My sister. I can't date until she does.\tSeems like she could get a date easy enough...\r\r\n" b'Why?\tUnsolved mystery. She used to be really popular when she started high school, then it was just like she got sick of it or something.\r\r\n' b"Unsolved mystery. She used to be really popular when she started high school, then it was just like she got sick of it or something.\tThat's a shame.\r\r\n" b'Gosh, if only we could find Kat a boyfriend...\tLet me see what I can do.\r\r\n'
在參考了LU敏的部落格後,發現在開啟檔案時,需要增加一個引數:newline=‘’
# Write new csv file
print("\nWriting newly formatted file...")
with open(datafile, 'w', newline='', encoding='utf-8') as outputfile:
writer = csv.writer(outputfile, delimiter=delimiter)
for pair in extractSentencePairs(conversations):
writer. writerow(pair)
現在輸出就沒有空行了:
Sample lines from file:
b"Can we make this quick? Roxanne Korrine and Andrew Barrett are having an incredibly horrendous public break- up on the quad. Again.\tWell, I thought we'd start with pronunciation, if that's okay with you.\r\n"
b"Well, I thought we'd start with pronunciation, if that's okay with you.\tNot the hacking and gagging and spitting part. Please.\r\n"
b"Not the hacking and gagging and spitting part. Please.\tOkay... then how 'bout we try out some French cuisine. Saturday? Night?\r\n"
b"You're asking me out. That's so cute. What's your name again?\tForget it.\r\n"
b"No, no, it's my fault -- we didn't have a proper introduction ---\tCameron.\r\n"
b"Cameron.\tThe thing is, Cameron -- I'm at the mercy of a particularly hideous breed of loser. My sister. I can't date until she does.\r\n"
b"The thing is, Cameron -- I'm at the mercy of a particularly hideous breed of loser. My sister. I can't date until she does.\tSeems like she could get a date easy enough...\r\n"
b'Why?\tUnsolved mystery. She used to be really popular when she started high school, then it was just like she got sick of it or something.\r\n'
b"Unsolved mystery. She used to be really popular when she started high school, then it was just like she got sick of it or something.\tThat's a shame.\r\n"
b'Gosh, if only we could find Kat a boyfriend...\tLet me see what I can do.\r\n'
看了一下這篇帖子的評論,教程裡的程式碼在Linux直接執行應該是沒問題的,因為“Linux的換行只有LF, 剛好和多出來的CR配合”(by zhzyx)
因為我是在windows下做的實驗,所以加上前面提到的引數也可以解決空行的問題