问题:Python字符串打印为[u’String’]

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

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

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

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

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

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.

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.

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?


回答 0

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

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

 soup[0].encode("ascii")

但是,请仔细检查您的数据是否真的是ASCII。这很少见。更有可能是latin-1或utf-8。

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


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

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

 soup[0].encode(soup.originalEncoding)

[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.

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")

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")

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

 soup[0].encode(soup.originalEncoding)

回答 1

您可能有一个包含一个unicode字符串的列表。的repr[u'String']

您可以使用以下任何变体将其转换为字节字符串列表:

# Functional style.
print map(lambda x: x.encode('ascii'), my_list)

# List comprehension.
print [x.encode('ascii') for x in my_list]

# Interesting if my_list may be a tuple or a string.
print type(my_list)(x.encode('ascii') for x in my_list)

# What do I care about the brackets anyway?
print ', '.join(repr(x.encode('ascii')) for x in my_list)

# That's actually not a good way of doing it.
print ' '.join(repr(x).lstrip('u')[1:-1] for x in my_list)

You probably have a list containing one unicode string. The repr of this is [u'String'].

You can convert this to a list of byte strings using any variation of the following:

# Functional style.
print map(lambda x: x.encode('ascii'), my_list)

# List comprehension.
print [x.encode('ascii') for x in my_list]

# Interesting if my_list may be a tuple or a string.
print type(my_list)(x.encode('ascii') for x in my_list)

# What do I care about the brackets anyway?
print ', '.join(repr(x.encode('ascii')) for x in my_list)

# That's actually not a good way of doing it.
print ' '.join(repr(x).lstrip('u')[1:-1] for x in my_list)

回答 2

import json, ast
r = {u'name': u'A', u'primary_key': 1}
ast.literal_eval(json.dumps(r)) 

将打印

{'name': 'A', 'primary_key': 1}
import json, ast
r = {u'name': u'A', u'primary_key': 1}
ast.literal_eval(json.dumps(r)) 

will print

{'name': 'A', 'primary_key': 1}

回答 3

如果访问/打印单个元素列表(例如顺序或过滤):

my_list = [u'String'] # sample element
my_list = [str(my_list[0])]

If accessing/printing single element lists (e.g., sequentially or filtered):

my_list = [u'String'] # sample element
my_list = [str(my_list[0])]

回答 4

将输出传递给str()函数,它将删除转换的unicode输出。同样通过打印输出,它将从中删除u”标签。

pass the output to str() function and it will remove the convert the unicode output. also by printing the output it will remove the u” tags from it.


回答 5

[u'String'] 是列表的文本表示形式,在Python 2上包含Unicode字符串。

如果运行print(some_list),则相当于
print'[%s]' % ', '.join(map(repr, some_list))创建类型为的Python对象的文本表示形式listrepr()即为每个项目调用函数。

请勿混淆Python对象及其文本表示形式repr('a') != 'a'甚至文本表示形式的文本表示形式也有所不同:repr(repr('a')) != repr('a')

repr(obj)返回一个字符串,其中包含对象的可打印表示形式。它的目的是在REPL中明确表示对象,这对于调试很有用。经常eval(repr(obj)) == obj

为避免调用repr(),您可以直接打印列表项(如果它们都是Unicode字符串),例如:print ",".join(some_list)—它以逗号分隔的形式列出字符串列表:String

不要使用硬编码字符编码将Unicode字符串编码为字节,而是直接打印Unicode。否则,代码可能会失败,因为编码无法代表所有字符,例如,如果您尝试对'ascii'非ASCII字符使用编码。或者,如果环境使用的编码与硬编码的编码不兼容,则代码会默默地产生mojibake(在管道中进一步传递损坏的数据)。

[u'String'] is a text representation of a list that contains a Unicode string on Python 2.

If you run print(some_list) then it is equivalent to
print'[%s]' % ', '.join(map(repr, some_list)) i.e., to create a text representation of a Python object with the type list, repr() function is called for each item.

Don’t confuse a Python object and its text representationrepr('a') != 'a' and even the text representation of the text representation differs: repr(repr('a')) != repr('a').

repr(obj) returns a string that contains a printable representation of an object. Its purpose is to be an unambiguous representation of an object that can be useful for debugging, in a REPL. Often eval(repr(obj)) == obj.

To avoid calling repr(), you could print list items directly (if they are all Unicode strings) e.g.: print ",".join(some_list)—it prints a comma separated list of the strings: String

Do not encode a Unicode string to bytes using a hardcoded character encoding, print Unicode directly instead. Otherwise, the code may fail because the encoding can’t represent all the characters e.g., if you try to use 'ascii' encoding with non-ascii characters. Or the code silently produces mojibake (corrupted data is passed further in a pipeline) if the environment uses an encoding that is incompatible with the hardcoded encoding.


回答 6

在“字符串”上使用dirtype找出其含义。我怀疑这是BeautifulSoup的标记对象之一,打印时像一个字符串,但实际上不是一个。否则,它在列表内,您需要分别转换每个字符串。

无论如何,您为什么反对使用Unicode?有什么具体原因吗?

Use dir or type on the ‘string’ to find out what it is. I suspect that it’s one of BeautifulSoup’s tag objects, that prints like a string, but really isn’t one. Otherwise, its inside a list and you need to convert each string separately.

In any case, why are you objecting to using Unicode? Any specific reason?


回答 7

你是真的意思u'String'

无论如何,您不能只是str(string)获取字符串而不是unicode字符串吗?(对于所有字符串均为unicode的Python 3,这应该有所不同。)

Do you really mean u'String'?

In any event, can’t you just do str(string) to get a string rather than a unicode-string? (This should be different for Python 3, for which all strings are unicode.)


回答 8

encode("latin-1") 就我而言对我有帮助:

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

encode("latin-1") helped me in my case:

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

回答 9

也许我不明白,为什么您不能只获取element.text然后在使用之前将其转换?例如(不知道为什么要这样做,但是…)找到网页的所有标签元素,并在它们之间进行迭代,直到找到一个名为MyText的元素为止。

        avail = []
        avail = driver.find_elements_by_class_name("label");
        for i in avail:
                if  i.text == "MyText":

从i转换字符串,然后执行您想做的任何事情……也许我在原始消息中缺少了什么?还是这是您想要的?

Maybe i dont understand , why cant you just get the element.text and then convert it before using it ? for instance (dont know why you would do this but…) find all label elements of the web page and iterate between them until you find one called MyText

        avail = []
        avail = driver.find_elements_by_class_name("label");
        for i in avail:
                if  i.text == "MyText":

Convert the string from i and do whatever you wanted to do … maybe im missing something in the original message ? or was this what you were looking for ?


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。