Reading rows of CSV file into string

Question:

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.

Asked By: user2330104

||

Answers:

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.

Answered By: Stephane Rolland
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.