1

I have a fixed length text document that I would like to parse and output to a CSV file and add a header to the newly created document that is not in the raw data. Below is the code I have for parsing based on column width.

import pandas as pd

path = '5010.txt'

col_specification =[(1, 12), (13, 18), (19, 26), (27, 31), (32, 39), (40, 47), (48, 57), (58, 62), (63, 67), (68, 72), (73, 77), (78, 78), (79, 83), (84, 84), (85, 86), (87, 92), 
(93, 97), (98, 98), (99, 103), (104, 104), (105, 106), (107, 112), (113, 120), (121, 128), (129, 129), (130, 131), (132, 134), (135, 139), (140, 155), (156, 171),(172, 187), (188, 203), 
(204, 213), (214, 221),]
data = pd.read_fwf(path, colspecs=col_specification)

How do I output data to a CSV file with a header?

2
  • What should the output look like? Should it be identical to 5010.txt but with a header? Commented Dec 30, 2014 at 20:16
  • Pascal, the output should be either a space or comma delimitated txt or CSV. So not identical it would parse the raw data based on the fixed length criteria and output a csv or text. Commented Dec 30, 2014 at 21:29

1 Answer 1

2

You can use the DataFrame's to_csv method:

data.to_csv("out_file.csv", header=["column1 title", "column2 title",...])

This will save the index (in effect, the row number) as the first column. To suppress this behaviour, pass the additional argument index=False to the method call.

Sign up to request clarification or add additional context in comments.

3 Comments

the data.to_csv("out_file.csv", index=False) will create a CSV properly formated. However when I try your header method the CSV is created but empty i.e no data or headers within the file.
This works for me. Does pandas throw an error when you try and save? Can you post the exact code that you are using to save the CSV file?
Your original suggestion works as you posted. I was incorrect I had a syntax error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.