简体   繁体   English

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

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

I have a script that scans an excel sheet and prints column A and a range of rows which outputs a list in python 2, but has the expected unicode character, which I can't seem to figure out how to get rid of after referring to several stack overflow posts.我有一个脚本可以扫描 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)

How do I remove the unicode u' from the list output?如何从列表输出中删除 unicode u'? Thanks in advance.提前致谢。

If you are seeing a u , you are using Python 2. In Python 2, print is a statement not a function, so don't call it with parentheses.如果您看到u ,则您使用的是 Python 2。在 Python 2 中, print是一个语句而不是一个函数,所以不要用括号调用它。 Calling with () make it print a tuple, and the default display for a tuple is to use repr() on the tuple's items.()调用使它打印一个元组,元组的默认显示是在元组的项目上使用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 will only print Unicode characters that are supported by the terminal encoding, so you might get the dreaded UnicodeEncodeError if you have Unicode code points outside your terminal's encoding default. Python 2 将只打印终端编码支持的 Unicode 字符,因此如果您有终端编码默认值之外的 Unicode 代码点,您可能会遇到可怕的UnicodeEncodeError Switch to Python 3 for better Unicode handling.切换到 Python 3 以获得更好的 Unicode 处理。 Python 2 is EOL. Python 2 已停产。

change below lines in your script:更改脚本中的以下几行:

option a:print(i, str(sheet.cell(row=i, column=2).value)) option b:a.print(i, (sheet.cell(row=i, column=2).value).encode('ascii',选项 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