簡體   English   中英

在Python中寫入.txt文件時嘗試將輸出字符串轉換為UTF-8

[英]Trying to convert output string to UTF-8 when writing to .txt file in Python

我正在嘗試將.txt文件中的行提交給Google翻譯api,然后將這些結果輸出到單獨的.txt文件中。 一切正常,除了當我讀取輸出文件時,它是unicode的,所以我最終得到了像/ xeda這樣的字符。 我試圖在寫入文件之前將結果轉換為utf-8,但是我的嘗試似乎沒有效果。 我沒有得到任何錯誤,但是我仍然得到了垃圾字符。 我的(相關)代碼如下所示:

read_array = []
write_array = []
write_file = 'write_file.txt'
read_file = open('metaphors1.txt','r')
s = codecs.open('write_file.txt', 'w', 'utf-8')

for line in read_file:
    #Reads sentences from the input file, converts them to a string with
    #all lowercase letters (to prevent garbage values then puts the strings
    #in an array
    readstring = str(line)
    readstring = readstring.lower()
    read_array.append(readstring)

for item in read_array:
    #removes new line symbols to prevent translation errors then submits
    #sentences in the array to the translator, then writes the sentences
    #to a new array
    readitem = str(item)
    readitem.rstrip('\n')
    results1 = translator.translate(readitem)
    resultstring = str(results1)
    write_array.append(resultstring)

for item in write_array:
    #writes the results to an output file
    writeitem = str(item)
    writeitem = writeitem.encode('utf-8')
    s.write("%s\n" % writeitem)

s.close()

我確信我做錯的任何事情都是簡單明了的,但是我對此深感困惑。 任何幫助,將不勝感激。 謝謝!

查看http://docs.python.org/2/library/stdtypes.html#str.decode ,即使您不關心錯誤,甚至可以告訴它忽略錯誤。

line.decode('utf-8','ignore')

暫無
暫無

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

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