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

  • Home
  • SEARCH
  • 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 730893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:57:50+00:00 2026-05-14T06:57:50+00:00

I’ve got a CSV file with a format that looks like this: FieldName1, FieldName2,

  • 0

I’ve got a CSV file with a format that looks like this:

“FieldName1”, “FieldName2”, “FieldName3”, “FieldName4”
“04/13/2010 14:45:07.008”, “7.59484916392”, “10”, “6.552373”
“04/13/2010 14:45:22.010”, “6.55478493312”, “9”, “3.5378543”
…

Note that there are double quote characters at the start and end of each line in the CSV file, and the "," string is used to delimit fields within each line. The number of fields in the CSV file can vary from file to file.

When I try to read this into numpy via:
import numpy as np
data = np.genfromtxt(csvfile, dtype=None, delimiter=',', names=True)
all the data gets read in as string values, surrounded by double-quote characters. Not unreasonable, but not much use to me as I then have to go back and convert every column to its correct type

When I use delimiter='","' instead, everything works as I’d like, except for the 1st and last fields. As the start of line and end of line characters are a single double-quote character, this isn’t seen as a valid delimiter for the 1st and last fields, so they get read in as e.g. "04/13/2010 14:45:07.008 and 6.552373" – note the leading and trailing double-quote characters respectively. Because of these redundant characters, numpy assumes the 1st and last fields are both String types; I don’t want that to be the case

Is there a way of instructing numpy to read in files formatted in this fashion as I’d like, without having to go back and “fix” the structure of the numpy array after the initial read?

  • 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-14T06:57:50+00:00Added an answer on May 14, 2026 at 6:57 am

    The basic problem is that NumPy doesn’t understand the concept of stripping quotes (whereas the csv module does). When you say delimiter='","', you’re telling NumPy that the column delimiter is literally a quoted comma, i.e. the quotes are around the comma, not the value, so the extra quotes you get on he first and last columns are expected.

    Looking at the function docs, I think you’ll need to set the converters parameter to strip quotes for you (the default does not):

    import re
    import numpy as np
    
    fieldFilter = re.compile(r'^"?([^"]*)"?$')
    def filterTheField(s):
        m = fieldFilter.match(s.strip())
        if m:
            return float(m.group(1))
        else:
            return 0.0 # or whatever default
    
    #...
    
    # Yes, sorry, you have to know the number of columns, since the NumPy docs
    # don't say you can specify a default converter for all columns.
    convs = dict((col, filterTheField) for col in range(numColumns))
    data = np.genfromtxt(csvfile, dtype=None, delimiter=',', names=True, 
        converters=convs)
    

    Or abandon np.genfromtxt() and let csv.csvreader give you the file’s contents a row at a time, as lists of strings, then you just iterate through the elements and build the matrix:

    reader = csv.csvreader(csvfile)
    result = np.array([[float(col) for col in row] for row in reader])
    # BTW, column headings are in reader.fieldnames at this point.
    

    EDIT: Okay, so it looks like your file isn’t all floats. In that case, you can set convs as needed in the genfromtxt case, or create a vector of conversion functions in the csv.csvreader case:

    reader = csv.csvreader(csvfile)
    converters = [datetime, float, int, float]
    result = np.array([[conv(col) for col, conv in zip(row, converters)] 
        for row in reader])
    # BTW, column headings are in reader.fieldnames at this point.
    

    EDIT 2: Okay, variable column count… Your data source just wants to make life difficult. Luckily, we can just use magic…

    reader = csv.csvreader(csvfile)
    result = np.array([[magic(col) for col in row] for row in reader])
    

    … where magic() is just a name I got off the top of my head for a function. (Psyche!)

    At worst, it could be something like:

    def magic(s):
        if '/' in s:
            return datetime(s)
        elif '.' in s:
            return float(s)
        else:
            return int(s)
    

    Maybe NumPy has a function that takes a string and returns a single element with the right type. numpy.fromstring() looks close, but it might interpret the space in your timestamps as a column separator.

    P.S. One downside with csvreader I see is that it doesn’t discard comments; real csv files don’t have comments.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
i got an object with contents of html markup in it, for example: string
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I want to count how many characters a certain string has in PHP, but
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.