0

This code pulls a little bit of data from RottenTomatoes. (Movie titles and tomato scores.) I'd like to now remove the the last 6 and first 3 characters from each item so only the movie title remains. I know that what I'm trying now would only remove the numbers if it worked. I don't know if what I'm trying is in the ballpark but it's saying I have a tuple when I thought I had a list so it may be more complicated than I understand.

Example of what I have...

[(u'Ouija: Origin of Evil', 82), (u'Long Way North (Tout en haut du
monde)', 98), (u'Come And Find Me', 67), (u'My Father, Die', 78),
(u"Roger Corman's Death Race 2050", 100), ...

What I'd like done to it (as a permanent change to the list & not just printed in a new way)...

[Ouija: Origin of Evil, Long Way North (Tout en haut du monde), Come
And Find Me, My Father, Die, Roger Corman's Death Race 2050"...

My code...

import requests

r = requests.get('https://www.rottentomatoes.com/api/private/v2.0/'
                 'browse?page=1&limit=30&type=dvd-top-rentals&'
                 'services=amazon%3Bamazon_prime%3Bfandango_now%'
                 '3Bhbo_go%3Bitunes%3Bnetflix_iw%3Bvudu&sortBy=popularity')

movies = []
data = r.json()
for result in data["results"]:
    movies.append((result["title"], result["tomatoScore"]))

list2 = [i for i in movies if i[1] >=60]
list2 = ' '.join(list2).replace('1''2''3''4''5''6''7''8''9','').split()

print list2

The error...

(venv) My-MacBook-Pro:tor Me$ python bs.py
Traceback (most recent call last):
  File "bs.py", line 16, in <module>
    list2 = ' '.join(list2).replace('1''2''3''4''5''6''7''8''9','').split()
TypeError: sequence item 0: expected string, tuple found
(venv) My-MacBook-Pro:tor Me$
martineau
  • 119,623
  • 25
  • 170
  • 301
Ant
  • 933
  • 2
  • 17
  • 33
  • 1
    You have a list of tuples. I think you just want `[t[0] for t in list2]` – juanpa.arrivillaga Jan 25 '17 at 23:24
  • Are you referring to the " (u' " in the result? You stored the data as a list of tuples, and as a string in it, python prints that. It refers to unicode. For reference http://stackoverflow.com/questions/2464959/whats-the-u-prefix-in-a-python-string – Dinesh.hmn Jan 25 '17 at 23:26

1 Answers1

0

You want the first item in each tuple contained in the list, like so:

>>> mylist = [
    (u'Ouija: Origin of Evil', 82),
    (u'Long Way North (Tout en haut dumonde)', 98),
    (u'Come And Find Me', 67), (u'My Father, Die', 78),
    (u"Roger Corman's Death Race 2050", 100) ]
>>> result = [i[0] for i in mylist]
>>> print result
>>> ['Ouija: Origin of Evil', 'Long Way North (Tout en haut dumonde)', 'Come And Find Me', 'My Father, Die', "Roger Corman's Death Race 2050"]
Apollo2020
  • 509
  • 2
  • 8
  • Sounds like there are non-ascii unicode characters in some of the data your are processing. I used str() in the list comprehension to try return a "nicely printable" string but, to be honest, it probably isn't necessary. I modified my example to remove it. The leading u before each string simply indicates that it is a unicode string. – Apollo2020 Jan 26 '17 at 01:05
  • 1
    It was some title with weird characters. If you run the code & pull the data from RottenTomatoes.com you'll see it's position [9]. I deleted it and I deleted my comment asking you about it. Thank you for pointing out the title and number were different items. I learned a lot here. – Ant Jan 26 '17 at 01:33