n00bie help with file input and parsing

Sean Ross sross at connectmail.carleton.ca
Sat May 10 11:58:13 EDT 2003


Hi.
This will do what you're looking for:

input = open('./input.txt')
for line in input:
    seperated = line.split(',')
    info = [s.replace('"','') for s in seperated]
    output = info[3]
    output = 'QH' + output + '.txt'
    out = open(output, 'w')
    for i in info:
        print >>out, i
    out.close()
input.close()

Sean



"jason" <j_bongo79 at hotmail.com> wrote in message
news:a900370d.0305100642.1c418930 at posting.google.com...
> Hi All,
>
> I am trying to write a program which takes the input from a csv file
> called input.txt.  This file contains the following:
>
> "1","2","3","10064299","5","6","7","8","9"
> "1","2","3","10064862","5","6","7","8","9"
> "1","2","3","10064908","5","6","7","8","9"
> "1","2","3","10161170","5","6","7","8","9"
> "1","2","3","10161925","5","6","7","8","9"
> .....
> etc.
>
> I also have files separate text files named QH10064299.txt,
> QH10064862.txt, QH10064908.txt etc.
>
> Basically, what I need to do is parse the "10064299" string from the
> input.txt file, strip the quotation marks, and add a QH and .txt onto
> the end so it matches one of other separate files.  Then I need to
> open this file up in append mode, and add all the other information to
> it.
>
> For example, if I pulled the 10064299 from the 1st line of input, I
> would then add the QH and .txt to make QH10064299.txt.  Then I would
> open this file for appending and append:
>
> 1
> 2
> 3
> 10064299
> 5
> 6
> 7
> 8
> 9
>
> NB all quotes removed.  Someone told me this could be written in about
> 10 lines of Python code, could anyone help me out?  I have never
> before used Python.
>
> Apologies for the long post.
>
> Thanks






More information about the Python-list mailing list