[英]Convert csv text from utf-16 to ascii or read in correctly
从csv文件中读取文本时遇到问题。 csv文件中的示例行如下所示:“
1477-7819-4-45-2 Angiolymphatic Invasion(H&E400Ã)。“
我想问题是文本的编码,所以我决定将其更改为ASCII。
到目前为止这是我的python代码:
text_path = '/some_path/filename.csv'
text_path_ascii = '/some_path/filename_ASCII.csv'
input_codec = 'UTF-16'
output_codec = 'ASCII'
for line in unicode_file:
unicode_data = unicode_file.read().decode(input_codec)
#here is another problem => AttributeError: 'str' object has no attribute 'decode'
unicode_data = unicode_file.read()
ascii_file = open(text_path_ascii, 'w')
ascii_file.write(unicode_data.write(unicode_data.encode(output_codec)))
# same problem=> AttributeError: 'str' object has no attribute 'encode'
ascii_file.write(unicode_data.encode(output_codec))
所以我的问题是我不知道如何编码/解码文本。
我甚至不确定这是否是处理错误书面文本的正确方法(是的,如果您使用任何编辑器打开文本,文本看起来像给定的行)。
或者是否有一种更简单的方法可以直接读取csv文本而没有“破碎”字符?
谢谢你的想法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.