[英]How to read csv files (with special characters) in Python? How can I decode the text data? Read encoded text from file and convert to string
[英]Python 3.6 - Read encoded text from file and convert to string
希望有人可以帮我解决以下问题。 它可能不是太复杂,但我无法弄明白。 我的“output.txt”文件创建时使用:
f = open('output.txt', 'w')
print(tweet['text'].encode('utf-8'))
print(tweet['created_at'][0:19].encode('utf-8'))
print(tweet['user']['name'].encode('utf-8'))
f.close()
如果我没有编写它来写入文件,它会给我错误。 所以“输出”包含3行utf-8编码输出:
b'testtesttest'
b'line2test'
b'\xca\x83\xc9\x94n ke\xc9\xaan'
在“main.py”中,我试图将其转换回字符串:
f = open("output.txt", "r", encoding="utf-8")
text = f.read()
print(text)
f.close()
不幸的是,b'' - 格式仍未删除。 我还需要解码吗? 如果可能的话,我想保留3排结构。 我为新手问题道歉,这是我在SO上的第一个问题:)
非常感谢你提前!
在人们回答我的问题的帮助下,我已经能够让它发挥作用。 解决方案是改变如何写入文件的方式:
tweet = json.loads(data)
tweet_text = tweet['text'] # content of the tweet
tweet_created_at = tweet['created_at'][0:19] # tweet created at
tweet_user = tweet['user']['name'] # tweet created by
with open('output.txt', 'w', encoding='utf-8') as f:
f.write(tweet_text + '\n')
f.write(tweet_created_at+ '\n')
f.write(tweet_user+ '\n')
然后读它像:
f = open("output.txt", "r", encoding='utf-8')
tweettext = f.read()
print(text)
f.close()
打开文件时不要指定编码,而是在阅读时使用它进行解码。
f = open("output.txt", "rb")
text = f.read().decode(encoding="utf-8")
print(text)
f.close()
如果b
和引号'
在您的文件中,则表示您的文件存在问题。 有人可能write(print(line))
而不是write(line)
。 现在解码它,你可以使用literal_eval
。 否则@m_callens的答案应该没问题。
import ast
with open("b.txt", "r") as f:
text = [ast.literal_eval(line) for line in f]
for l in text:
print(l.decode('utf-8'))
# testtesttest
# line2test
# ʃɔn keɪn
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.