Saturday, June 24, 2017

Pandas - How to read text files delimited with fixed widths

With Python Pandas library it is possible to easily read fixed width text files, for example:


In this case, the text file has its first 4 lines without data and the 5th line with the header. The header and the data are delimeted with fixed char widths, being the widths sizes as following:
  •  12 spaces , 10 spaces ,6 spaces ,9 spaces ,7 spaces,7 spaces ,7 spaces ,4 spaces
The following code will read the file as a pandas DataFrame, and also parse the dates in the datetime format:

import pandas as pd

ds2 = pd.read_fwf('yourtextfile.txt', widths=[12,10,6,9,7,7,7,4], skiprows=4, parse_dates=True)



12 comments:

  1. Replies
    1. sorry not replying on time! There is no function to_fwf.. we have to use python workarounds..

      Delete
  2. I like your blog, I read this blog please update more content on python, further check it once at python online training

    ReplyDelete
  3. I like your blog, I read this blog please update more content on python, further check it once at python online training

    ReplyDelete
  4. A Plain Text Editor
    Plain Text files
    That's right, if you're writer on a budget, you don't need to spend any money buying expensive writing software or apps. Instead, you can use the text editor that comes free with your operating system.
    Just open up Notepad on Windows or TextEdit on a Mac. I like plain text editors for writing something short quickly and easily, without thinking much about it. I wrote a blog post about the benefits of using plain text editors as writing software.
    Use for: writing whatever, wherever

    ReplyDelete
  5. A Plain Text Editor
    Plain Text files
    That's right, if you're writer on a budget, you don't need to spend any money buying expensive writing software or apps. Instead, you can use the text editor that comes free with your operating system.
    Just open up Notepad on Windows or TextEdit on a Mac. I like plain text editors for writing something short quickly and easily, without thinking much about it. I wrote a blog post about the benefits of using plain text editors as writing software.
    Use for: writing whatever, wherever

    ReplyDelete
  6. It was very helpful and informative blog. Thanks for sharing it us.
    Python Course in Nagpur

    ReplyDelete