Python 新闻列表

Python News List

我正在尝试从 CNN 创建一个新闻列表并使用下面的当前代码工作正常并给我这个输出:

America is doing better than we're being led to believe
2016 candidates' daughters step up on trail this week
Paralympics day seven: Highlights from Rio
When athletes can take drugs. What are Therapeutic Use Exemptions?
Israeli security agents partner with Volkswagen to make unhackable cars
Will Warren Buffett break his silence on Wells Fargo?
Caitlyn Jenner visits Hill to raise LGBT awareness
Businesses could pose conflicts of interest for a President Trump
UNC football player turns himself in on sexual battery charge
EU faces 'existential crisis,' warns European Commission head
Melania Trump releases letter from immigration attorney

代码如下:

feed = feedparser.parse(cnnUrl)
incrementOne = 0
newsList = []

print "" #Formatting Purposes
print "News:"
for post in feed.entries:
   if incrementOne < 11:
         print post.title
         incrementOne = incrementOne + 1
     newsList.append(post.title)

#print newsList

问题是,一旦我取消注释 newsList(最后一行)以用于程序的另一部分,它就会给我一个我不想要的带有 "u" 的输出:

America is doing better than we're being led to believe
2016 candidates' daughters step up on trail this week
Paralympics day seven: Highlights from Rio
When athletes can take drugs. What are Therapeutic Use Exemptions?
Israeli security agents partner with Volkswagen to make unhackable cars
Will Warren Buffett break his silence on Wells Fargo?
Caitlyn Jenner visits Hill to raise LGBT awareness
Businesses could pose conflicts of interest for a President Trump
UNC football player turns himself in on sexual battery charge
EU faces 'existential crisis,' warns European Commission head
Melania Trump releases letter from immigration attorney
[u"America is doing better than we're being led to believe", u"2016 candidates' daughters step up on trail this week", u'Paralympics day seven: Highlights from Rio', u'When athletes can take drugs. What are Therapeutic Use Exemptions?', u'Israeli security agents partner with Volkswagen to make unhackable cars', u'Will Warren Buffett break his silence on Wells Fargo?', u'Caitlyn Jenner visits Hill to raise LGBT awareness', u'Businesses could pose conflicts of interest for a President Trump', u'UNC football player turns himself in on sexual battery charge', u"EU faces 'existential crisis,' warns European Commission head", u'Melania Trump releases letter from immigration attorney']

开头有一个"u",然后在每个新闻标题前重复。

我的问题是为什么会这样,我该如何解决

字符串前的 u 前缀只是告诉你它们是 unicode 字符串(报纸使用的不仅仅是 ascii 字符)。如果你真的打印每个字符串,这个 u 就不会出现;它只是告诉你字符串是用 unicode 编码的。

试试这个:

for news in newsList:
    print news

print '\n'.join(newsList)

您会注意到 u 实际上并没有出现在打印中。 Here 是 Python.

中 unicode 字符串的文档

您正在打印数组的表示形式(更准确地说,转换为 strrepr 可能是不同的输出)

如果要打印没有 unicode、引号和括号的列表,只需

print("\n".join(newsList))

这会将列表连接成一个字符串,以换行符分隔。

发生的事情是你告诉解释器打印一个列表,并且解释器通过调用它的 repr 来尽最大努力来表示 Unicode 字符串列表,这涉及到 hte repr 每个元素。

如果您只想查看(比方说)逗号分隔的字符串值列表,那么您应该使用

print ", ".join(newsList)

您可以使用任何您喜欢的分隔符字符串来代替上面示例中的 ", "