-1

I have a string of id's in numbers saved as CSVs

enter link description here

I want to convert each row in this csv file to be a string/list

mylist=[411211, 1234404, 5711427, 13600442, 13600641, 13601660, 13619537, 15302899 ...]

Then each numbers on this string will go through an API request and then spit out Names

Then I want to be able to store these names in csv again.

I know the code for converting numbers to names via API works because I tried to manually type in the numbers as lists in python and was able to print out names in python. But I'm having a trouble working with csv...


Converting into list worked.

import urllib2
import json
import csv

with open('jfl42facebooknumberid.csv', 'rU') as csvfile:
    reader = csv.reader(csvfile, quoting=csv.QUOTE_NONE)
    
    for row in reader:
        myid='. '.join(row)
        try:
            myfile=urllib2.urlopen("http://graph.facebook.com/"+ myid +"?fields=name")
            myjson=json.loads(myfile.read())
            print myjson["name"]
        except:
            print myid

But this prints me the results I want! I just need to figure out how to store into a csv.

4
  • 1
    Also in Python 2.x, docs.python.org/2/library/csv.html Commented Apr 28, 2013 at 21:39
  • 1
    I know I know sorry I was looking at it for the longest time but I kept getting weird results... sorry really new to python but the first part worked! I just have to store printed results as csv file... Commented Apr 28, 2013 at 21:51
  • I know why actually.. it was because I kept using rb when I had to use rU (I still don't get what the difference between the two is...) Commented Apr 28, 2013 at 21:52
  • See docs.python.org/2/library/functions.html#open . rb is for non-text files so not for CSV. rU handles newlines correctly even if the CSV file was written on a different OS, with a different newline convention, than the OS you are running Python on. Commented Apr 28, 2013 at 23:36

1 Answer 1

4

if you go to csv in docs python, the example is:

import csv
with open('eggs.csv', 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in spamreader:
        print ', '.join(row)

I think it is a great and simple example.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.