Unicode语法中的歧义XlsxWriter Python 2模块



我现在正在使用python 2.7.6并使用XslxWriter模块将一些数据写入excel文件。我有一些unicode数据存储在一个列表中。当我尝试根据文档使用说明将数据保存到文件时,我遇到了一些麻烦。指令发现这里

指令说,当你想使用unicode字符串和python 2的模块时,你必须在字符串的第一个输入u !我尝试用简单的字符串,如محسن。结果很好。

但是我的数据在一个列表中,当我尝试这样做时,解释器尝试将其识别为具有新名称的变量,例如ufoo。当空格断开时,解释器会将u识别为未知变量(也可以尝试将+连接在一起,这样就会出现问题)我有点糊涂了。有办法解决这种歧义吗?

谢谢你的帮助。

我使用decode函数。

tip = 'Haz clic aquí para acceder al fichero remoto.'
tip = tip.decode('utf-8')

在python 2中使用decode的一个示例

import xlsxwriter
# Create a new workbook and add a worksheet
workbook = xlsxwriter.Workbook('hyperlink.xlsx')
worksheet = workbook.add_worksheet('Hyperlinks')
# Format the first column
worksheet.set_column('A:A', 30)
# Add the standard url link format.
url_format = workbook.add_format({
        'font_color': 'blue',
        'underline':  1
})
# Add a sample alternative link format.
red_format = workbook.add_format({
        'font_color': 'red',
        'bold':       1,
        'underline':  1,
        'font_size':  12,
})
# Add an alternate description string to the URL.
string = 'Grabación'
string = string.decode('utf-8')
# Add a "tool tip" to the URL.
tip = 'Haz clic aquí para acceder al fichero remoto.'
tip = tip.decode('utf-8')
# Write some hyperlinks
worksheet.write_url('A15', 'external://ordenador-remoto/Directorio/fichero.wav', red_format, string, tip)
workbook.close()    

最新更新