-1

I am trying to create a news list from CNN and using the current code below it works fine and gives me this output:

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

Here is the code:

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

The problem is that once I uncomment newsList (the last line) to use for another part of the program, it gives me an output with a "u" that I don't want:

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']

At the beginning, there is a "u" and then it repeats before every news headline.

My question is why is this happening and how can I fix

0

3 Answers 3

1

you are printing the representation of the array (more precisely the conversion to str, repr may be a different output)

If you want to print the list without the unicode, quotes and brackets, just

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

this joins the list into a string, separated by linefeeds.

0

The u prefix before the strings is just telling you that they are unicode strings (newspapers use more than just ascii characters). If you actually print each string, this u will not show up; it is just there to tell you that the strings are encoded with unicode.

Try this:

for news in newsList:
    print news

or

print '\n'.join(newsList)

You'll notice the u doesn't actually show up in the printing. Here is the doc for unicode strings in Python.

0

What's happening is that your are telling the interpreter to print a list, and the interpreter is doing its best to represent a list of Unicode strings by calling for its repr, which involves taking hte repr of each element.

If you just want to see (say) a comma-separated list of the string values then you should use

print ", ".join(newsList)

You can use any separator string you like instead of ", " in the example above.

Not the answer you're looking for? Browse other questions tagged or ask your own question.