Editing particular lines of a text file.

Discussion in 'Python' started by Shriphani, Oct 9, 2007.

  1. Shriphani

    Shriphani Guest

    Hello all,

    I am trying to create a script that looks at specific strings in a
    file like:

    msgid "I am a disco dancer."

    and compares the part in quotes to the keys in a dictionary, finds the
    value and creates a new line right after this string in the file. I
    have planned to write this as follows:

    1. Open the file in read mode
    2. parse each line to figure out which line contains "msgid" and use
    the shlex module's split method to go and split this line and pick the
    2nd element list[1].
    3. find the value from the dictionary corresponding to the above
    element.
    4. Insert the line. This part is where I face a problem. How do I
    plainly edit just one line. I would also like to look at some sample
    code that does this.
    5. open a new file and write the new file with the inserted strings to
    it.
    6. close both files opened.

    Regards,
    Shriphani Palakodety
     
    Shriphani, Oct 9, 2007
    #1
    1. Advertising

  2. Shriphani

    Tim Williams Guest

    On 09/10/2007, Shriphani <> wrote:
    > Hello all,
    >
    > I am trying to create a script that looks at specific strings in a
    > file like:
    >
    > msgid "I am a disco dancer."
    >
    > and compares the part in quotes to the keys in a dictionary, finds the
    > value and creates a new line right after this string in the file. I
    > have planned to write this as follows:
    >
    > 1. Open the file in read mode
    > 2. parse each line to figure out which line contains "msgid" and use
    > the shlex module's split method to go and split this line and pick the
    > 2nd element list[1].
    > 3. find the value from the dictionary corresponding to the above
    > element.
    > 4. Insert the line. This part is where I face a problem. How do I
    > plainly edit just one line. I would also like to look at some sample
    > code that does this.
    > 5. open a new file and write the new file with the inserted strings to
    > it.
    > 6. close both files opened.



    infile = open('infile.txt')
    outfile = open('outfile.txt','w')
    for line in infile:
    if 'msgid' in line:
    # transform line
    # make sure the line ending is intact
    outfile.write(line)
    infile.close()
    outfile.close()

    or maybe

    infile = open('infile.txt')
    outfile = open('outfile.txt','w')
    new_file = []
    for line in infile:
    if 'msgid' in line:
    # transform line
    # make sure the line ending is intact
    new_file.append(line)
    outfile.write(''.join(new_file)
    infile.close()
    outfile.close()




    --

    Tim Williams
     
    Tim Williams, Oct 9, 2007
    #2
    1. Advertising

  3. Shriphani

    Larry Bates Guest

    Shriphani wrote:
    > Hello all,
    >
    > I am trying to create a script that looks at specific strings in a
    > file like:
    >
    > msgid "I am a disco dancer."
    >
    > and compares the part in quotes to the keys in a dictionary, finds the
    > value and creates a new line right after this string in the file. I
    > have planned to write this as follows:
    >
    > 1. Open the file in read mode
    > 2. parse each line to figure out which line contains "msgid" and use
    > the shlex module's split method to go and split this line and pick the
    > 2nd element list[1].
    > 3. find the value from the dictionary corresponding to the above
    > element.
    > 4. Insert the line. This part is where I face a problem. How do I
    > plainly edit just one line. I would also like to look at some sample
    > code that does this.
    > 5. open a new file and write the new file with the inserted strings to
    > it.
    > 6. close both files opened.
    >
    > Regards,
    > Shriphani Palakodety
    >


    Sounds like homework, but I'm feeling generous (not tested).

    xlate={'"I am a disco dancer."':'"but John Travolta is better"'}


    fp1=open('inputfile.txt', 'r')
    fp2=open('outputfile.txt', 'w')

    for line in fp1:
    #
    # Your description is unclear here about whether the new line
    # replaces the existing one or is inserted after it.
    #
    fp2.writeline(line)
    if line.startswith('msgid'):
    parts=line.split(' ')
    try: parts[1]=xlate[parts[1]]
    except:
    #
    # Handle exception if your translation dictionary does
    # not have the string you are looking for here.
    #
    raise KeyError

    newline=' '.join(parts)
    fp2.writeline(newline)

    fp1.close()
    fp2.close()


    -Larry
     
    Larry Bates, Oct 9, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Wright
    Replies:
    0
    Views:
    554
    Joe Wright
    Jul 27, 2003
  2. Replies:
    0
    Views:
    616
  3. Murali
    Replies:
    2
    Views:
    611
    Jerry Coffin
    Mar 9, 2006
  4. MENTAT
    Replies:
    5
    Views:
    148
    John W. Krahn
    Feb 15, 2005
  5. Cah Sableng
    Replies:
    0
    Views:
    263
    Cah Sableng
    Apr 23, 2007
Loading...

Share This Page