pythonpython-2.7csvfixed-length-recordfixed-length-file

how to convert fix length file to csv file in python?


In the file each columns have fix size.

Is there any library or function available using that we can convert easily to CSV file ?

There are four columns in the file.

1st - 1233212Q1AQYHDVCS1221
2nd - 64342343EDV53234212
3rd - 11123321111124122
4th - 52122211

Sample records :

1233212Q1AQYHDVCS1221 64342343EDV53234212 11123321111124122
52122211

1233212Q1AQYHDVCS1221 64342343EDV53234212 11123321111124122
52122211

1233212Q1AQYHDVCS1221 64342343EDV53234212 11123321111124122
52122211

1233212Q1AQYHDVCS1221 64342343EDV53234212 11123321111124122
52122211

1233212Q1AQYHDVCS1221 64342343EDV53234212 11123321111124122
52122211

I would like to convert straight forward that file to CSV.


Solution

  • If the columns are separated by whitespace, simple sed can do it:

    sed 's/[\t ]+/,/g' input.txt > output.txt