I'm a newbie! I have a non-delimited data file that I'd like to convert to delimited.
Example... Line in non-delimited file: 0139725635999992000010100534+42050-102800FM-15+1198KAIA
Should be: 0139,725635,99999,2000,01,01,00,53,4,+42050,-102800,FM-15,+1198,KAIA
What is the best way to go about this? I've looked all over for examples, help, suggestions, but have not found much. CSV module doesn't seem to do exactly what I want. Maybe I'm just missing something or not using the correct terminology in my searches. Any assistance is greatly appreaciated! Using Python 2.4
> I'm a newbie! I have a non-delimited data file that I'd like to > convert to delimited.
> Example... > Line in non-delimited file: > 0139725635999992000010100534+42050-102800FM-15+1198KAIA
> Should be: > 0139,725635,99999,2000,01,01,00,53,4,+42050,-102800,FM-15,+1198,KAIA
> What is the best way to go about this? I've looked all over for > examples, help, suggestions, but have not found much. CSV module > doesn't seem to do exactly what I want. Maybe I'm just missing > something or not using the correct terminology in my searches. Any > assistance is greatly appreaciated! Using Python 2.4
Since you have to know, a priori, how to break the input string I assume that these fields are of fixed length. You can use the following to do what you want:
> > I'm a newbie! I have a non-delimited data file that I'd like to > > convert to delimited.
> > Example... > > Line in non-delimited file: > > 0139725635999992000010100534+42050-102800FM-15+1198KAIA
> > Should be: > > 0139,725635,99999,2000,01,01,00,53,4,+42050,-102800,FM-15,+1198,KAIA
> > What is the best way to go about this? I've looked all over for > > examples, help, suggestions, but have not found much. CSV module > > doesn't seem to do exactly what I want. Maybe I'm just missing > > something or not using the correct terminology in my searches. Any > > assistance is greatly appreaciated! Using Python 2.4
> Since you have to know, a priori, how to break the input string I > assume that these fields are of fixed length. You can use the following > to do what you want:
On Aug 27, 10:59 am, RyanL <ryanlaurit...@gmail.com> wrote:
> I'm a newbie! I have a non-delimited data file that I'd like to > convert to delimited.
> Example... > Line in non-delimited file: > 0139725635999992000010100534+42050-102800FM-15+1198KAIA
> Should be: > 0139,725635,99999,2000,01,01,00,53,4,+42050,-102800,FM-15,+1198,KAIA
> What is the best way to go about this? I've looked all over for > examples, help, suggestions, but have not found much. CSV module > doesn't seem to do exactly what I want. Maybe I'm just missing > something or not using the correct terminology in my searches. Any > assistance is greatly appreaciated! Using Python 2.4
I don't think you are going to find anything that will just do this for you. You are going to have read the file, figure out where to split the string, and reprint it delimited with commas. As for suggesting code... I can't tell how you actually want to delimit the stuff from the above example? Are the fields always a fixed number of characters? If they aren't then is there some other method for determining how many characters to group into a field? From the looks of it you could split that string any way you want and get something that looks right, but isn't.
> I'm a newbie! I have a non-delimited data file that I'd like to > convert to delimited.
> Example... > Line in non-delimited file: > 0139725635999992000010100534+42050-102800FM-15+1198KAIA
> Should be: > 0139,725635,99999,2000,01,01,00,53,4,+42050,-102800,FM-15,+1198,KAIA
> What is the best way to go about this? I've looked all over for > examples, help, suggestions, but have not found much. CSV module > doesn't seem to do exactly what I want. Maybe I'm just missing > something or not using the correct terminology in my searches. Any > assistance is greatly appreaciated! Using Python 2.4
On Aug 27, 12:59 pm, RyanL <ryanlaurit...@gmail.com> wrote:
> I'm a newbie! I have a non-delimited data file that I'd like to > convert to delimited.
> Example... > Line in non-delimited file: > 0139725635999992000010100534+42050-102800FM-15+1198KAIA
> Should be: > 0139,725635,99999,2000,01,01,00,53,4,+42050,-102800,FM-15,+1198,KAIA
> What is the best way to go about this? I've looked all over for > examples, help, suggestions, but have not found much. CSV module > doesn't seem to do exactly what I want. Maybe I'm just missing > something or not using the correct terminology in my searches. Any > assistance is greatly appreaciated! Using Python 2.4
I'm guessing that these lines *aren't* fixed-length, especially those signed integer fields. I used the patented Paul McGuire CrystalBall module to come up with this pyparsing rendition. (OP may adjust to suit.)
-- Paul
data = "0139725635999992000010100534+42050-102800FM-15+1198KAIA" """to be parsed as: 0139,725635,99999,2000,01,01,00,53,4,+42050,-102800,FM-15,+1198,KAIA"""
from pyparsing import * import time def convertTimeStamp(t): t["date"] = map(int,t.date) t["time"] = map(int,t.time) return time.strftime("%Y-%m-%dT%H:%M", tuple(t.date)+tuple(t.time)+(0,0,0,0))