简体   繁体   中英

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.

(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? 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. Calling with () make it print a tuple, and the default display for a tuple is to use repr() on the tuple's items.

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. Switch to Python 3 for better Unicode handling. Python 2 is EOL.

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',

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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