字符串之前的U [英] The u before strings

查看:246
本文介绍了字符串之前的U的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用beautifulsoap我从分析一个html表中的一些值如下:

Using beautifulsoap I had parsed some values from an html table as follows:

for string in soup.stripped_strings:
    all_tds.append(string)

在我简单的打印字符串作为

when I simply print the strings as

for string in soup.stripped_strings:
    print string

我得到在页面中的所有字符串,但是当我在一个列表其存储在1片断,然后打印列表我在前面的字符串获得一个U(UNI code),如下

I get all the strings in the page but when I store it in a list as in 1st snippet and then print the list I get a u(unicode) in front of the string as below

[u'stormvirux (4pa11cs031)', u'Semester:', u'5', u'Result:\xa0\xa0FIRST CLASS', u'Subject', u'External', u'Internal', u'Total', u'Result', u'Software Engineering (10IS51)', u'58', u'24', u'82', u'P', u'Systems Software (10CS52)', u'70', u'24', u'94', u'P', u'Operating Systems (10CS53)', u'58', u'18', u'76', u'P', u'Database Management Systems (10CS54)', u'42', u'25', u'67', u'P', u'Computer Networks - I (10CS55)', u'62', u'23', u'85', u'P', u'Formal Languages & Automata Theory (10CS56)', u'37', u'24', u'61', u'P', u'Database Applications Laboratory (10CSL57)', u'40', u'25', u'65', u'P', u'Systems Software & Operating Systems Lab. (10CSL58)', u'40', u'21', u'61', u'P', u'Total Marks:', u'591']

如果我尝试写字符串直接到文件 fl.write(串)我得到的错误

If I try to write string directly to file fl.write(string) I get the error

UnicodeEncodeError: 'ascii' codec can't encode characters in position 7-8: ordinal not in range(128)

这是怎么回事,什么可以做?

Why is this happening and what can be done?

推荐答案

使用 codecs.open() io.open( )使用合适的文本编码(即编码=打开一个文本文件...),而不是用<$ C打开bytefile $ C>的open()。

Use codecs.open() or io.open() to open a text file using an appropriate text encoding (i.e. encoding="...") instead of opening a bytefile with open().

这篇关于字符串之前的U的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆