Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8633207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:27:29+00:00 2026-06-12T09:27:29+00:00

I’m trying to export some API JSON data to a csv, but I only

  • 0

I’m trying to export some API JSON data to a csv, but I only want some bits of it. I’ve tried row.append, but I’m getting the error TypeError: string indices must be integers.

I’m new to python, and a bit confused as to why it wants an integer.

import urllib2
import json
import csv

outfile_path='/NYTComments.csv'

writer = csv.writer(open(outfile_path, 'w'))

url = urllib2.Request('http://api.nytimes.com/svc/community/v2/comments/recent?api-key=ea7aac6c5d0723d7f1e06c8035d27305:5:66594855')

parsed_json = json.load(urllib2.urlopen(url))

print parsed_json

for comment in parsed_json['results']:
    row = []
    row.append(str(comment['commentSequence'].encode('utf-8')))
    row.append(str(comment['commentBody'].encode('utf-8')))
    row.append(str(comment['commentTitle'].encode('utf-8')))
    row.append(str(comment['approveDate'].encode('utf-8')))
    writer.writerow(row)

The parsed_json printed looks like this:

{u'status': u'OK',
u'results':
    {u'totalCommentsReturned': 25,
    u'comments':
        [{
            u'status': u'approved',
            u'sharing': 0,
            u'approveDate': u'1349378866',
            u'display_name': u'Seymour B Moore',
            u'userTitle': None,
            u'userURL': None,
            u'replies': [],
            u'parentID': None,
            u'articleURL': u'http://fifthdown.blogs.nytimes.com/2012/10/03/thursday-matchup-cardinals-vs-rams/',
            u'location': u'SoCal',
            u'userComments': u'api.nytimes.com/svc/community/v2/comments/user/id/26434659.xml',
            u'commentSequence': 2,
            u'editorsSelection': 0,
            u'times_people': 1,
            u'email_status': u'0',
            u'commentBody': u"I know most people won't think this is a must watch game, but it will go a long way .... (truncated)",
            u'recommendationCount': 0,
            u'commentTitle': u'n/a'
        }]
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:27:30+00:00Added an answer on June 12, 2026 at 9:27 am
    for comment in parsed_json['results']: #'comment' contains keys, not values
    

    should be

    for comment in parsed_json['results']['comments']:
    

    parsed_json['results'] itself is another dictionary.

    parsed_json['results']['comments'] is the list of dictionaries you want to iterate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to construct a data frame in an Rcpp function, but when I
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.