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 8399323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:16:19+00:00 2026-06-09T21:16:19+00:00

I am attempting to query the twitter search engine (search.twitter.com), convert the results into

  • 0

I am attempting to query the twitter search engine (search.twitter.com), convert the results into json, and then prepare the results as a csv for a research project. I am a python novice, but I have managed to code 2/3 of the program myself. However, I have a difficult time converting my json file into the csv format. I have tried various suggested techniques without success. What am I doing wrong here?

Here is what I have so far:

import twitter, os, json, csv

qname = raw_input("Please enter the term(s) you wish to search for: ")
date = int(raw_input("Please enter today's date (no dashes or spaces): "))
nname = raw_input("Please enter a nickname for this query (no spaces): ")
q1 = raw_input("Would you like to set a custom directory? Enter Yes or No: ")

if q1 == 'No' or 'no' or 'n' or 'N':
    dirname = 'C:\Users\isaac\Desktop\TPOP'

elif q1 == 'Yes' or 'yes' or 'y' or 'Y':
    dirname = raw_input("Please enter the directory path:")

ready = raw_input("Are you ready to begin? Enter Yes or No: ")
while ready == 'Yes' or 'yes' or 'y' or 'Y':
    twitter_search = twitter.Twitter(domain = "search.Twitter.com")
search_results = []
for page in range (1,10):
    search_results.append(twitter_search.search(q=qname, rpp=1, page=page))
    ready1 = raw_input("Done! Are you ready to continue? Enter Yes or No: ")
    if ready1 == 'Yes' or 'yes' or 'y' or 'Y':
        break

ready3 = raw_input("Do you want to save output as a file? Enter Yes or No: ")
while ready3 == 'Yes' or 'yes' or 'y' or 'Y':
    os.chdir(dirname)
    filename = 'results.%s.%06d.json' %(nname,date)
    t = open (filename, 'wb+')
    s = json.dumps(search_results, sort_keys=True, indent=2)
    print >> t,s
    t.close()
    ready4 = raw_input("Done! Are you ready to continue? Enter Yes or No: ")
    if ready4 == 'Yes' or 'yes' or 'y' or 'Y':
        break

ready5 = raw_input("Do you want to save output as a csv/excel file? Enter Yes or No: ")
while ready5 == 'Yes' or 'yes' or 'y' or 'Y':
    filename2 = 'results.%s.%06d.csv' %(nname,date)
    z = json.dumps(search_results, sort_keys=True, indent=2)
    x=json.loads(z)

    json_string = z
    json_array = x

    columns = set()
    for entity in json_array:
        if entity == "created_at" or "from_user" or "from_user_id" or "from_user_name" or "geo" or "id" or "id_str" or "iso_language_code" or "text":
            columns.update(set(entity))

    writer = csv.writer(open(filename2, 'wb+'))
    writer.writerow(list(columns))
    for entity in json_array:
        row = []
        for c in columns:
            if c in entity: row.append(str(entity[c]))
            else: row.append('')
  • 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-09T21:16:21+00:00Added an answer on June 9, 2026 at 9:16 pm

    After some searching around, I found the answer here: http://michelleminkoff.com/2011/02/01/making-the-structured-usable-transform-json-into-a-csv/

    The code should look something like this:(if you are search the twitter python api)

    filename2 = '/path/to/my/file.csv'
    writer = csv.writer(open(filename2, 'w'))
    z = json.dumps(search_results, sort_keys=True, indent=2)
    parsed_json=json.loads(z)
    #X needs to be the number of page you pulled less one. So 5 pages would be 4.
    while n<X:
     for tweet in parsed_json[n]['results']:
         row = []
         row.append(str(tweet['from_user'].encode('utf-8')))
         row.append(str(tweet['created_at'].encode('utf-8')))
         row.append(str(tweet['text'].encode('utf-8')))
         writer.writerow(row)
     n = n +1
    

    Thanks Everyone for the help!

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

Sidebar

Related Questions

I am attempting to write the results from an SQL query into text files.
I'm attempting to grab the search query from search engine referrals. Most pass the
I am attempting to convert the following HQL query to Criteria. from SubportfolioAudit as
I am attempting to pass a text box value into a SQL query, something
I'm writing a simple diagnostic query then attempting to execute it in the Oracle
When attempting to enable search query logging from the Shared Services Provider of my
I'm attempting to write a simple query where I declare some variables and then
I am attempting to split the ORDER BY statement of a SQL query into
I'm attempting to run an SQL query to display the results of a set
I am attempting to pull several @user and #hashtags from the twitter search API.

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.