Split file into several and reformat

B

bomber71

Hi,

I want to take read an input file (sels.txt) that looks like:

Begin sels
sel1 = {1001, 1002, 1003, ...
...
1099}

sel2 = {1001, 1008, 1009 ...
...
1299}
End sels

And turn it into an output file for each of the "sels" in the input file, i.e
sel1.txt:

L1001
L1002
L1003
....
L1099

and sel2.txt:

L1001
L1008
L1009
....
L1299

And so on. Many thanks,
Wayne
 
H

half.italian

Hi,

I want to take read an input file (sels.txt) that looks like:

Begin sels
sel1 = {1001, 1002, 1003, ...
...
1099}

sel2 = {1001, 1008, 1009 ...
...
1299}
End sels

And turn it into an output file for each of the "sels" in the input file, i.e
sel1.txt:

L1001
L1002
L1003
...
L1099

and sel2.txt:

L1001
L1008
L1009
...
L1299

And so on. Many thanks,
Wayne

hehe...

After Bruno's suggested read, go here: http://docs.python.org/tut/

Look at the string module.. specifically .strip() and .split(",")
Also look at reading files and perhaps lists.

for line in open("filename.txt", 'r').readlines():
handle_the_line()

Good Luck.

~Sean
 
N

Neil Cerutti

Hi,

I want to take read an input file (sels.txt) that looks like:

Begin sels
sel1 = {1001, 1002, 1003, ...
...
1099}

sel2 = {1001, 1008, 1009 ...
...
1299}
End sels

And turn it into an output file for each of the "sels" in the input file, i.e
sel1.txt:

L1001
L1002
L1003
...
L1099

and sel2.txt:

L1001
L1008
L1009
...
L1299

And so on. Many thanks,

I think I'd put together a simple grammar and then write a
recursive descent parser that spit out my output files. But
that's just because I find that kind of thing fun. ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top