繁体   English   中英

从 python2 脚本列表输出中删除 u' unicode

[英]Remove u' unicode from python2 script list output

我有一个脚本可以扫描 Excel 工作表并打印 A 列和一系列行,这些行在 python 2 中输出一个列表,但具有预期的 unicode 字符,我似乎无法弄清楚如何在参考后摆脱几个堆栈溢出帖子。

(6188, u'machine1')
(6189, u'machine2')
import openpyxl
wb = openpyxl.load_workbook('AnsibleReadinessReviewIP.xlsx')
sheet = wb['CIsIPaddresses']
sheet['A301']
sheet['A301'].value
A = sheet['A301']
A.value
sheet.cell(row=301, column=A)
# Get the row, column, and vlaue from the cell
'Row %s, Column %s is %s' % (A.row, A.column, A.value)
'Cell %s is %s' % (A.coordinate, A.value)
sheet['A301']
for i in range('301, 6197'):
    print(i, sheet.cell(row=i, column=2).value)

如何从列表输出中删除 unicode u'? 提前致谢。

如果您看到u ,则您使用的是 Python 2。在 Python 2 中, print是一个语句而不是一个函数,所以不要用括号调用它。 ()调用使它打印一个元组,元组的默认显示是在元组的项目上使用repr()

Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> i=6188
>>> value = u'machine1'
>>> print(i,value)  # prints a tuple, which uses `repr()` for items.
(6188, u'machine1')
>>> print i,value   # prints values using `str()`, which won't display `u`.
6188 machine1      

Python 2 将只打印终端编码支持的 Unicode 字符,因此如果您有终端编码默认值之外的 Unicode 代码点,您可能会遇到可怕的UnicodeEncodeError 切换到 Python 3 以获得更好的 Unicode 处理。 Python 2 已停产。

更改脚本中的以下几行:

选项 a:print(i, str(sheet.cell(row=i, column=2).value)) 选项 b:a.print(i, (sheet.cell(row=i, column=2).value) .encode('ascii',

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM