使用Python通过CSV阅读韩语

2024-10-03 17:16:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个问题,读取一个CSV文件到Python包含英语和韩语字符,已经测试了我的代码没有朝鲜语,它工作得很好。在

代码(Python-3.6.4)

import csv
with open('Kor3.csv', 'r') as f:
    reader = csv.reader(f)
    your_list = list(reader)
print(your_list)

错误

return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 2176: character maps to undefined

CSV文件输出:此文件已从Excel转换为unicode文本,然后文件名更改为CSV。认为这就是问题所在。 从Excel或其他格式阅读会更好吗?在

样本输入(2列)

생일축하해요生日快乐


Tags: 文件csv代码importyourwithopen字符
3条回答

使用python 3。默认情况下,Csv函数将使用unicode进行读取

最后,我只是从excel文件导入,认为这是csv而不是python的问题。谢谢你的帮助。在

from xlrd import open_workbook

wb = open_workbook('Korean.xlsx')
values = []
for s in wb.sheets():
    #print 'Sheet:',s.name
    for row in range(1, s.nrows):
        col_names = s.row(0)
        col_value = []
        for name, col in zip(col_names, range(s.ncols)):
            value  = (s.cell(row,col).value)
            try : value = str(int(value))
            except : pass
            col_value.append((value))
        values.append(col_value)
print(values) #test
print(values[0][1],values[1][1]) #test2

打开文件时只需声明编码:

with open('Kor3.csv', 'r', encoding='utf-8') as f:

相关问题 更多 >