Python字符串打印为[u'String'] [英] Python string prints as [u'String']

查看:99
本文介绍了Python字符串打印为[u'String']的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这肯定是一件容易的事,但这确实困扰着我.

This will surely be an easy one but it is really bugging me.

我有一个脚本,该脚本可以读取网页并使用 Beautiful Soup 进行解析.从中提取所有链接,因为我的最终目标是打印出link.contents.

I have a script that reads in a webpage and uses Beautiful Soup to parse it. From the soup I extract all the links as my final goal is to print out the link.contents.

我要解析的所有文本都是ASCII.我知道Python将字符串视为unicode,并且我确信这非常方便,在我的wee脚本中没有用.

All of the text that I am parsing is ASCII. I know that Python treats strings as unicode, and I am sure this is very handy, just of no use in my wee script.

每次我去打印一个包含'String'的变量时,都会在屏幕上显示[u'String'].是否有一种简单的方法可以将其恢复为ascii,还是应该编写一个正则表达式来删除它?

Every time I go to print out a variable that holds 'String' I get [u'String'] printed to the screen. Is there a simple way of getting this back into just ascii or should I write a regex to strip it?

推荐答案

[u'ABC']将是一元字符串的unicode字符串. 美丽汤"始终会产生Unicode .因此,您需要将列表转换为单个unicode字符串,然后将其转换为ASCII.

[u'ABC'] would be a one-element list of unicode strings. Beautiful Soup always produces Unicode. So you need to convert the list to a single unicode string, and then convert that to ASCII.

我不知道您是如何得到一元素清单的; content成员将是字符串和标签的列表,这显然不是您所拥有的.假设您确实总是得到一个包含单个元素的列表,并且您的测试实际上仅是 ASCII,则可以使用以下代码:

I don't know exaxtly how you got the one-element lists; the contents member would be a list of strings and tags, which is apparently not what you have. Assuming that you really always get a list with a single element, and that your test is really only ASCII you would use this:

 soup[0].encode("ascii")

但是,请仔细检查您的数据是否真的是ASCII.这是非常罕见的.更有可能是latin-1或utf-8.

However, please double-check that your data is really ASCII. This is pretty rare. Much more likely it's latin-1 or utf-8.

 soup[0].encode("latin-1")


 soup[0].encode("utf-8")

或者您询问Beautiful Soup,原始编码是什么,然后以该编码重新获得:

Or you ask Beautiful Soup what the original encoding was and get it back in this encoding:

 soup[0].encode(soup.originalEncoding)

这篇关于Python字符串打印为[u'String']的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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