File I/O

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kirt

    #1

    File I/O

    Hi! I need some help in file I/O

    I have an xml file..

    <Top>
    <Body1>xyz</Body1>
    </Top>

    I want to open these file in append mode and add the line
    <Body2>abd</Body2>

    Such that i have:

    <Top>
    <Body1>xyz</Body1>
    <Body2>xyz</Body2>
    </Top>

    can anyone show me the way to achieve this??

    thanx

  • jimburton

    #2
    Re: File I/O


    Kirt wrote:
    Hi! I need some help in file I/O
    >
    I have an xml file..
    [snip]
    See http://diveintopython.org/xml_processing/

    Comment

    • Kirt

      #3
      Re: File I/O


      jimburton wrote:
      Kirt wrote:
      Hi! I need some help in file I/O

      I have an xml file..
      [snip]
      See http://diveintopython.org/xml_processing/
      i dont wanna parse the xml file..

      Just open the file as:

      f=open('test.xm l','a')

      and write a line "<Body2>abc </Body2>" before tag </Top?

      Comment

      • Kirt

        #4
        Re: File I/O


        jimburton wrote:
        Kirt wrote:
        Hi! I need some help in file I/O

        I have an xml file..
        [snip]
        See http://diveintopython.org/xml_processing/
        i dont wanna parse the xml file..

        Just open the file as:

        f=open('test.xm l','a')

        and append a line "<Body2>abc </Body2>" before tag </Top>

        Comment

        • jimburton

          #5
          Re: File I/O


          Kirt wrote:
          i dont wanna parse the xml file..
          >
          Just open the file as:
          >
          f=open('test.xm l','a')
          >
          and append a line "<Body2>abc </Body2>" before tag </Top>
          Use a regex to split the contents and insert new stuff, eg

          import re
          prog = prog = re.compile('^(. *)(</Top>)', re.DOTALL)
          m = prog.search(f.r ead())

          then m.group(1) is everything before </Topand m.group(2) is </Top>.
          Put them together with your new tag and write it back to the file

          Comment

          • Diez B. Roggisch

            #6
            Re: File I/O

            Kirt wrote:
            >
            jimburton wrote:
            >Kirt wrote:
            Hi! I need some help in file I/O
            >
            I have an xml file..
            >[snip]
            >See http://diveintopython.org/xml_processing/
            >
            i dont wanna parse the xml file..
            If you play soccer, do you insist on playing with a baseball bat?

            The game is called XML, and to play it, you should use a parser. If you
            don't, you're up for trouble.

            Diez

            Comment

            • jimburton

              #7
              Re: File I/O


              Diez B. Roggisch wrote:
              Kirt wrote:
              >

              jimburton wrote:
              Kirt wrote:
              Hi! I need some help in file I/O

              I have an xml file..
              [snip]
              See http://diveintopython.org/xml_processing/
              i dont wanna parse the xml file..
              >
              If you play soccer, do you insist on playing with a baseball bat?
              >
              I've been known to open a bottle of wine with a pencil. Works fine.

              Comment

              • Diez B. Roggisch

                #8
                Re: File I/O

                jimburton wrote:
                >
                Diez B. Roggisch wrote:
                >Kirt wrote:
                >>
                >
                jimburton wrote:
                >Kirt wrote:
                Hi! I need some help in file I/O
                >
                I have an xml file..
                >[snip]
                >See http://diveintopython.org/xml_processing/
                >
                i dont wanna parse the xml file..
                >>
                >If you play soccer, do you insist on playing with a baseball bat?
                >>
                I've been known to open a bottle of wine with a pencil. Works fine.
                Good to know that you can get drunk under any circumstances.

                I have seen a bazillion bad xml/html parsing-hacks using regexes and the
                like, which stopped working after somehow the xml came all in one line, or
                some other implicit assumption about its layout failed to be met.




                Comment

                • MonkeeSage

                  #9
                  Re: File I/O

                  Diez B. Roggisch wrote:
                  Good to know that you can get drunk under any circumstances.
                  +(++)1 heh!
                  I have seen a bazillion bad xml/html parsing-hacks using regexes and the
                  like, which stopped working after somehow the xml came all in one line, or
                  some other implicit assumption about its layout failed to be met.
                  Seriously, I fully agree with you. To manipulate X/HT/ML data reliably
                  you need to use a parser. And usually the parser API is much cleaner
                  and easier to work with than trying to cobble together regexps anyhow!

                  Regards,
                  Jordan

                  Comment

                  • Ant

                    #10
                    Re: File I/O


                    Kirt wrote:
                    ....
                    i dont wanna parse the xml file..
                    >
                    Just open the file as:
                    >
                    f=open('test.xm l','a')
                    >
                    and append a line "<Body2>abc </Body2>" before tag </Top>
                    The other guys are right - you should look at something like
                    ElementTree which makes this sort of thing pretty easy, and is robust.
                    But if you are sure that:

                    1) </Topis going to be on its own line (rather than something like
                    </Body1></Top>)
                    2) the ending tag will *definitely* have no extraneous whitespace (e.g.
                    < / Top >)

                    then the following will probably be the simplest solution:

                    f=open('test.xm l')
                    out = []
                    for line in f:
                    if "</Top>" in line:
                    out.append("<Bo dy2>abc</Body2>")
                    out.append(line ")

                    f.close()

                    f_out = open("test.xml" , "w")
                    f.write("".join (out))
                    f_out.close()

                    If you're working with XML in any sort of quantity, then look into the
                    available XML tools - it will be worth your time.

                    Comment

                    • uche.ogbuji@gmail.com

                      #11
                      Re: File I/O

                      Ant wrote:
                      Kirt wrote:
                      ...
                      i dont wanna parse the xml file..

                      Just open the file as:

                      f=open('test.xm l','a')

                      and append a line "<Body2>abc </Body2>" before tag </Top>
                      >
                      The other guys are right - you should look at something like
                      ElementTree which makes this sort of thing pretty easy, and is robust.
                      But if you are sure that:
                      >
                      1) </Topis going to be on its own line (rather than something like
                      </Body1></Top>)
                      2) the ending tag will *definitely* have no extraneous whitespace (e.g.
                      < / Top >)
                      >
                      then the following will probably be the simplest solution:
                      >
                      f=open('test.xm l')
                      out = []
                      for line in f:
                      if "</Top>" in line:
                      out.append("<Bo dy2>abc</Body2>")
                      out.append(line ")
                      >
                      f.close()
                      >
                      f_out = open("test.xml" , "w")
                      f.write("".join (out))
                      f_out.close()
                      And the most dangerous solution. Start with the line
                      "out.append(lin e")"

                      And have a look at the many failure possibilities I detail here:

                      www.XML.com,Textuality Services,Uche Ogbuji,Instruction, Programming,Proper XML Output in Python


                      Then add to that the fact that "</Top>" can legitimately appear in an
                      XML comment, so that logic is even more brittle.

                      The following code does this *safely* with Amara:

                      import amara
                      doc = amara.parse('te st.xml')
                      top = doc.xml_xpath('//Top')[0]
                      top.xml_parent. xml_insert_befo re(top, doc.xml_create_ element(u'Body2 ',
                      content=u'abc') )
                      top.xml(stream= open('test.xml' , 'w'))

                      Amara: http://uche.ogbuji.net/tech/4suite/amara/


                      --
                      Uche Ogbuji Fourthought, Inc.
                      http://uche.ogbuji.net http://fourthought.com
                      http://copia.ogbuji.net http://4Suite.org
                      Articles: http://uche.ogbuji.net/tech/publications/

                      Comment

                      Working...