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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:13:09+00:00 2026-06-10T09:13:09+00:00

I want to read a dataframe from a fixed width flat file. This is

  • 0

I want to read a dataframe from a fixed width flat file. This is a somewhat performance sensitive operation.

I would like all blank whitespace to be stripped from column value. After that whitespace is stripped, I want blank strings to be converted to NaN or None values. Here are the two ideas I had:

pd.read_fwf(path, colspecs=markers, names=columns,
            converters=create_convert_dict(columns))

def create_convert_dict(columns):
    convert_dict = {}
    for col in columns:
        convert_dict[col] = null_convert
        return convert_dict

def null_convert(value):
    value = value.strip()
    if value == "":
        return None
    else:
        return value

or:

pd.read_fwf(path, colspecs=markers, names=columns, na_values='',
            converters=create_convert_dict(columns))

def create_convert_dict(columns):
    convert_dict = {}
    for col in columns:
        convert_dict[col] = col_strip
    return convert_dict

def col_strip(value):
    return value.strip()

The second option depends on the converter (which strips whitespace) be evaluated before na_values.

I was wondering if the second one would work. The reason I am curious is because it seems better to retain NaN has the Null value opposed to None.

I am also open to any other suggestions for how I might perform this operation (stripping whitespace and then converting blank strings to NaN).

I do not have access to a computer with pandas installed at the moment, which is why I cannot test this myself.

  • 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-10T09:13:11+00:00Added an answer on June 10, 2026 at 9:13 am

    In case of fixed width file, no need to do anything special to strip white space, or handle missing fields. Below a small example of a fixed width file, three columns each of width 5. There is trailing and leading white space + missing data.

    In [57]: data = """\
    A    B     C     
     0    foo       
    3    bar     2.0
      1        3.0
    """
    
    In [58]: df = pandas.read_fwf(StringIO(data), widths=[5, 5, 5])
    
    In [59]: df
    Out[59]: 
       A    B   C
    0  0  foo NaN
    1  3  bar   2
    2  1  NaN   3
    
    In [60]: df.dtypes
    Out[60]: 
    A      int64
    B     object
    C    float64
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a data frame that I read from a file like this: name,
Hi I can't understand this error I want read this XML file: <?xml version=1.0
I want to read a INI file from python, and I came up with
I want to read the pdf file from raw folder if devices have any
I have a text file. I want read that file. But In that if
i want to read an html file by using jquery get function, replace some
I want to read a file piece by piece. The file is split up
I want to read .ppt files from the iPhone mail app. I have already
I want to read a xls file and then parse it. How can i
I want to read data from a website with cURL but from a certain

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.