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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:11:26+00:00 2026-05-19T22:11:26+00:00

I’m working with a CSV file in python, which will have ~100,000 rows when

  • 0

I’m working with a CSV file in python, which will have ~100,000 rows when in use. Each row has a set of dimensions (as strings) and a single metric (float).

As csv.DictReader or csv.reader return values as string only, I’m currently iterating over all rows and converting the one numeric value to a float.

for i in csvDict:
    i[col] = float(i[col])

Is there a better way that anyone could suggest to do this? I’ve been playing around with various combinations of map, izip, itertools and have searched extensively for some samples of doing it more efficiently, but unfortunately haven’t had much success.

In case it helps:
I’m doing this on appengine. I believe that what I’m doing may be resulting in me hitting this error:
Exceeded soft process size limit with 267.789 MB after servicing 11 requests total – I only get it when the CSV is quite large.

Edit: My Goal
I’m parsing this CSV so that I can use it as a data source for the Google Visualizations API. The final data set will be loaded in to a gviz DataTable for querying. Type must be specified during the construction of this table. My problem could also be solved if anyone knew of a good gviz csv->datatable converter in python!

Edit2: My Code

I believe that my issue has to do with the way I attempt to fixCsvTypes(). Also, data_table.LoadData() expects an iterable object.

class GvizFromCsv(object):
  """Convert CSV to Gviz ready objects."""

  def __init__(self, csvFile, dateTimeFormat=None):
    self.fileObj = StringIO.StringIO(csvFile)
    self.csvDict = list(csv.DictReader(self.fileObj))
    self.dateTimeFormat = dateTimeFormat
    self.headers = {}
    self.ParseHeaders()
    self.fixCsvTypes()

  def IsNumber(self, st):
    try:
        float(st)
        return True
    except ValueError:
        return False

  def IsDate(self, st):
    try:
      datetime.datetime.strptime(st, self.dateTimeFormat)
    except ValueError:
      return False

  def ParseHeaders(self):
    """Attempts to figure out header types for gviz, based on first row"""
    for k, v in self.csvDict[0].items():
      if self.IsNumber(v):
        self.headers[k] = 'number'
      elif self.dateTimeFormat and self.IsDate(v):
        self.headers[k] = 'date'
      else:
        self.headers[k] = 'string'

  def fixCsvTypes(self):
    """Only fixes numbers."""
    update_to_numbers = []
    for k,v in self.headers.items():
      if v == 'number':
        update_to_numbers.append(k)
    for i in self.csvDict:
      for col in update_to_numbers:
        i[col] = float(i[col])

  def CreateDataTable(self):
    """creates a gviz data table"""
    data_table = gviz_api.DataTable(self.headers)
    data_table.LoadData(self.csvDict)
    return data_table
  • 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-05-19T22:11:26+00:00Added an answer on May 19, 2026 at 10:11 pm

    I had first exploited the CSV file with a regex, but since the data in the file is very strictly arranged in each row, we can simply use the split() function

    import gviz_api
    
    scheme = [('col1','string','SURNAME'),('col2','number','ONE'),('col3','number','TWO')]
    data_table = gviz_api.DataTable(scheme)
    
    #  --- lines in surnames.csv are : --- 
    #  surname,percent,cumulative percent,rank\n
    #  SMITH,1.006,1.006,1,\n
    #  JOHNSON,0.810,1.816,2,\n
    #  WILLIAMS,0.699,2.515,3,\n
    
    with open('surnames.csv') as f:
    
        def transf(surname,x,y):
            return (surname,float(x),float(y))
    
        f.readline()
        # to skip the first line surname,percent,cumulative percent,rank\n
    
        data_table.LoadData( transf(*line.split(',')[0:3]) for line in f )
        # to populate the data table by iterating in the CSV file
    

    Or without a function to be defined:

    import gviz_api
    
    scheme = [('col1','string','SURNAME'),('col2','number','ONE'),('col3','number','TWO')]
    data_table = gviz_api.DataTable(scheme)
    
    #  --- lines in surnames.csv are : --- 
    #  surname,percent,cumulative percent,rank\n
    #  SMITH,1.006,1.006,1,\n
    #  JOHNSON,0.810,1.816,2,\n
    #  WILLIAMS,0.699,2.515,3,\n
    
    with open('surnames.csv') as f:
    
        f.readline()
        # to skip the first line surname,percent,cumulative percent,rank\n
    
        datdata_table.LoadData( [el if n==0 else float(el) for n,el in enumerate(line.split(',')[0:3])] for line in f )    
        # to populate the data table by iterating in the CSV file
    

    At one moment, I believed I was obliged to populate the data table with one row at a time because I was using a regex and that needed to obtain the matches’ groups before floating the numbers’ strings. With split() all can be done in one instruction with LoadData()

    .

    Hence, your code can be shortened. By the way, I don’t see why it should continue to define a class. Instead, a function seems enough for me:

    def GvizFromCsv(filename):
      """ creates a gviz data table from a CSV file """
    
      data_table = gviz_api.DataTable([('col1','string','SURNAME'),
                                       ('col2','number','ONE'    ),
                                       ('col3','number','TWO'    ) ])
    
      #  --- with such a table schema , lines in the file must be like that: ---  
      #  blah, number, number, ...anything else...\n 
      #  SMITH,1.006,1.006, ...anything else...\n 
      #  JOHNSON,0.810,1.816, ...anything else...\n 
      #  WILLIAMS,0.699,2.515, ...anything else...\n
    
      with open(filename) as f:
        data_table.LoadData( [el if n==0 else float(el) for n,el in enumerate(line.split(',')[0:3])]
                             for line in f )
      return data_table
    

    .

    Now you must examine if the way in which the CSV data is read from another API can be inserted in this code to keep the iterating principle to populate the data table.

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

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.