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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:37:27+00:00 2026-06-15T18:37:27+00:00

I am trying to create a .csv file with data that I have stored

  • 0

I am trying to create a .csv file with data that I have stored into a list from Twitter search API. I have saved the last 100 tweets with a keyword that I chose (in this case ‘reddit’) and I am trying to save each tweet into a cell in a .csv file. My code is below and I am returning an error that is:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 0: ordinal not in range(128)

If anyone knows what I can do to fix this it would be greatly appreciated!

import sys
import os


import urllib
import urllib2
import json
from pprint import pprint
import csv

import sentiment_analyzer

import codecs

class Twitter:
    def __init__(self):
        self.api_url = {}
        self.api_url['search'] = 'http://search.twitter.com/search.json?'

    def search(self, params):

        url = self.make_url(params, apitype='search')
        data = json.loads(urllib2.urlopen(url).read().decode('utf-8').encode('ascii',     'ignore'))

        txt = []
        for obj in data['results']:
            txt.append(obj['text'])

        return '\n'.join(txt)

    def make_url(self, params, apitype='search'):


        baseurl = self.api_url[apitype] 
        return baseurl + urllib.urlencode(params)


if __name__ == '__main__':
    try:
        query = sys.argv[1]
    except IndexError:
        query = 'reddit'

    t = Twitter()

    s = sentiment_analyzer.SentimentAnalyzer()

    params = {'q': query, 'result_type': 'recent', 'rpp': 100}

    urlName = t.make_url(params)
    print urlName
    txt = t.search(params)

    print s.analyze_text(txt)

    myfile = open('reddit.csv', 'wb')
    wr = csv.writer(myfile, quoting=csv.QUOTE_MINIMAL)
    wr.writerow(txt)
  • 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-15T18:37:28+00:00Added an answer on June 15, 2026 at 6:37 pm

    From the Python 2 documentation for the csv module:

    Note

    This version of the csv module doesn’t support Unicode input. Also,
    there are currently some issues regarding ASCII NUL characters.
    Accordingly, all input should be UTF-8 or printable ASCII to be safe;
    see the examples in section Examples.

    That said, you can probably parse the .csv file yourself without too much difficulty using Python’s built-in Unicode string support — there’s also this answer.

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

Sidebar

Related Questions

I am trying to create a csv file of some data. I have wrote
I'm trying to load data from a CSV file into a MySQL database, and
I am trying to create a CSV file from a dynamically generated table.I have
I'm trying to create a simple Silverlight application that involves parsing a CSV file
I am trying to create a csv file using python that is truly Excel-compatible
I was trying to create a quick little script that would insert data into
I have a time series(a csv file) data that looks like below. Each observation
I have a simple .csv file that has that I want to extract data
I am trying to create a CSV file with a list of URLs. I
I have an unnormalized events-diary CSV from a client that I'm trying to load

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.