python列表中的额外字符

Mat*_*hew 1 python selenium-webdriver

我遇到了一个问题:

slots = rows[i].find_elements_by_tag_name('td')
prodFolder = slots[0].text
prodType = slots[2].text
prodId = slots[1].text
values = [prodFolder, prodId, prodType]
print values
Run Code Online (Sandbox Code Playgroud)

当我去打印值时,我在列表中每个项目的前面得到一个额外的字符:
[u'active_e',u'1193',u'Active E']
这可能是.text提供一些额外数据的结果我不想要的.有没有一种优雅的方法来解决这个问题?(不使用蛮力去除额外的你?)

sch*_*ggl 5

'u'u'active_e'只是表明这是一个unicode对象,而不是一个字节串.你可以encode用来转换它:

> u = u'active_e'
> s = u.encode('utf-8')

> u
u'active_e'
> s
'active_e'

# But:
> print(u)
active_e
> print(s)
active_e

> type(u)
<type 'unicode'>
> type(s)
<type 'str'>
Run Code Online (Sandbox Code Playgroud)

但在大多数情况下,unicode对象和字节串一样精细.对于纯ASCII字符串,甚至u == s将是True:

> u == s
True

# careful with non-ascii chars:
> u = u'äöüß'
> s = u.encode('utf-8')
> u == s
False

> len(u)
4
> len(s)
8  # ä,ö,ü,ß have two-byte representations
Run Code Online (Sandbox Code Playgroud)