2016-11-30 46 views
0

我有這樣一個字符串:保存在一個文件中(python3)使用表情符號的字符串

the_str = 'acuerda piolin usado dieron vez amigo secreto jajaja ' 

如果我嘗試將其保存:

with open("some_name.txt", "a", encoding='UTF-8') as document: 
    dociment.write(the_str) 

我得到一個空的.txt文件。我該如何解決這個問題?我試圖做the_str.encode('utf-8'),但結果不是一個str,而是一個字節變量,如果我通過「ab」更改「a」,那麼我得到一個can't concat bytes to str錯誤。

感謝您的幫助。

回答

1

我想你的代碼,它的工作,改變docimentdocument

the_str = 'acuerda piolin usado dieron vez amigo secreto jajaja ' 
with open("some_name.txt", "a") as document: 
    document.write(the_str) 

我的輸出: enter image description here

+0

我的代碼是一個有點複雜,它是當我在寫這一個錯字。但它簡化了我寫的內容。 –

+0

@VladimirVargas編寫完整的代碼,因爲使用這段代碼我沒有問題 – eyllanesc

+0

我發現了錯誤。我有很多句子,如果句子符合條件,那麼句子就寫出來了。不過,我首先打開文件,然後檢查條件,從而創建一個空白文件。我應該做的是檢查條件,然後寫入文件。對於那個很抱歉。 –