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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:23:39+00:00 2026-05-15T04:23:39+00:00

the following function parses a CSV file into a list of dictionaries, where each

  • 0

the following function parses a CSV file into a list of dictionaries, where each element in the list is a dictionary where the values are indexed by the header of the file (assumed to be the first line.)

this function is very very slow, taking ~6 seconds for a file that’s relatively small (less than 30,000 lines.)

how can I speed it up?

def csv2dictlist_raw(filename, delimiter='\t'):
    f = open(filename)
    header_line = f.readline().strip()
    header_fields = header_line.split(delimiter)
    dictlist = []
    # convert data to list of dictionaries
    for line in f:
    values = map(tryEval, line.strip().split(delimiter))
    dictline = dict(zip(header_fields, values))
    dictlist.append(dictline)
    return (dictlist, header_fields)

in response to comments:

I know there’s a csv module and I can use it like this:

data = csv.DictReader(my_csvfile, delimiter=delimiter)

this is much faster. However, the problem is that it doesn’t automatically cast things that are obviously floats and integers to be numeric and instead makes them strings. How can I fix this?

Using the “Sniffer” class does not work for me. When I try it on my files, I get the error:

File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/csv.py", line 180, in sniff
    raise Error, "Could not determine delimiter"
Error: Could not determine delimiter

How can I make DictReader parse the fields into their types when it’s obvious?

thanks.

thanks.

  • 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-15T04:23:39+00:00Added an answer on May 15, 2026 at 4:23 am
    import ast
    
    # find field types
    for row in csv.DictReader(my_csvfile, delimiter=delimiter):
        break
    else:
        assert 0, "no rows to process"
    cast = {}
    for k, v in row.iteritems():
        for f in (int, float, ast.literal_eval):
            try: 
                f(v)
                cast[k] = f
                break
            except (ValueError, SyntaxError):
                pass
        else: # no suitable conversion
            cast[k] = lambda x: x.decode(encoding)
    
    # read data
    my_csvfile.seek(0)
    
    data = [dict((k.decode(encoding), cast[k](v)) for k, v in row.iteritems())
            for row in csv.DictReader(my_csvfile, delimiter=delimiter)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following function is trying to find the nth to last element of a
I parse message data into a CSV file via Outlook rules. How can I
I have a .csv file with records written in the following form: 2010-04-20 15:15:00,8.9915176259e+00,8.8562623697e+00
I have the following piece of code to parse a csv file. After that
The purpose: This function parses through a string trie following the path that matches
I'm using the following code to run a function that parses my xml files...
I've got the following code below which parses a text file and indexes the
The following block of code successfully returns array values (No. 1 130879253000, 34756) function
I have written a function that parses a url and splits it into it's
Suppose I want to make a function that recursively parses a variadic argument list,

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.