我在python上使用xlrd从excel导入一堆数据
我在字符串中得到的所有数据都是这样的:text:u'L \ xc9GENDE'
我操纵这些数据并尝试将它们放回excel中(使用xlsxwriter),当我这样做时,我会得到相同的文本文本块:u'L \ xc9GENDE'而不是LÉGENDE。
什么对我有用:
#!/usr/bin/env python
# -*- coding: latin-1 -*-
import xlsxwriter
import sys
workbook = xlsxwriter.Workbook('hello.xlsx')
worksheet = workbook.add_worksheet()
data = u'L\xc9GENDE'
worksheet.write('A1',data)
workbook.close()
这将有效,我将在A1单元格中获得LÉGENDE
但如果我试图操纵一个字符串,我已经给你了\ x \ xc9GENDE',它只会在A1单元格中显示L \ xc9GENDE
----编辑---- 我用来从excel中检索数据的代码
from xlrd import open_workbook
def grabexcelfile():
wb = open_workbook('leg.xls',encoding_override='latin-1')
log = []
txt = ''
for s in wb.sheets():
for row in range(s.nrows):
values = []
for col in range(s.ncols):
txt = str(s.cell(row,col))
txt.replace('-',' ',10)
log.append(txt)
return log
x = grabexcelfile()
print type(x[0]),x[0]
印刷品给了我:text:u'L \ xc9GENDE'
答案 0 :(得分:0)
试试这个。
import unicodedata
data = u'L\xc9GENDE'
unicodedata.normalize('NFKD',data).encode('ascii','ignore')
您可以在此处参考更多信息 - > Convert a Unicode string to a string in Python (containing extra symbols)
答案 1 :(得分:0)
而不是试图操纵文本:u'L \ xc9GENDE',而是改变了var excel的类型给了我这个:
from xlrd import open_workbook
def grabexcelfile():
wb = open_workbook('leg.xls',encoding_override='latin-1')
log = []
txt = ''
for s in wb.sheets():
for row in range(s.nrows):
values = []
for col in range(s.ncols):
#next line is changed
txt = sheet.cell(row,col).value
txt.replace('-',' ',10)
log.append(txt)
return log
x = grabexcelfile()
print type(x[0]),x[0]