How to use brackets in Python?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Thebuilderofdoom
    New Member
    • Mar 2010
    • 18

    How to use brackets in Python?

    Code:
    nubr1 = 0
    s = open("C:/DsE2/T1.txt").read()
    
    while nubr1 <=5:
        lib1l[nubr1] = open("C:/DsE2/lib001.txt").readlines()[nubr1]
        lib2l[nubr1] = open("C:/DsE2/lib002.txt").readlines()[nubr1]
        s = s.replace( lib1l[nubr1] ,lib2l[nubr1] )
        nubr1 = nubr1 + 1
    
    
    f = open("C:/DsE2/T1.txt", 'w')
    f.write(s)
    f.close()
    is this how you use brackets?

    ---How this program works---
    reads C:/DsE2/lib002.txt,C:/DsE2/lib001.txt,C:/DsE2/T1.txt
    replace words in C:/DsE2/T1.txt with line one in C:/DsE2/lib001.txt to line one in
    C:/DsE2/lib002.txt then replaces words in C:/DsE2/T1.txt with line two in C:/DsE2/lib001.txt to line two in C:/DsE2/lib002.txt and soo on...
    -------------------------------------------
    Last edited by bvdet; Mar 15 '10, 02:29 AM. Reason: Add indentation
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Brackets are tokens used as delimiters for lists, index of sequence types, and the slicing operator. I don't think your method will achieve the results you want. file method read() reads the entire file as one string. You could iterate on the file object (for line in fileObj:) and use file method next() on the other two files, so you could do whatever you need to line 1 of each file, then line 2 of each file, and so on.

    Comment

    • Thebuilderofdoom
      New Member
      • Mar 2010
      • 18

      #3
      i don't know half of what you saying? please explain in a more simpler

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        A list:
        Code:
        [1,2,3,4,]
        An element of a list and string accessed by index:
        Code:
        >>> [1,2,3,4][1]
        2
        >>> '12345'[-1]
        '5'
        >>>
        A slice of a sequence type (list, tuple, string)
        Code:
        >>> [1,2,3,4][1]
        2
        >>> '12345'[-1]
        '5'
        >>> [1,2,3,4][1:3]
        [2, 3]
        >>> '12345'[::-1]
        '54321'
        >>> '12345'[1:3]
        '23'
        >>>
        Here's an example of reading two text files at the same time:
        Code:
        f1 = open("file1.txt")
        f2 = open("file2.txt")
        for line in f1:
            print line.strip()
            try:
                line2 = f2.next()
                print line2.strip()
            except StopIteration, e:
                print "Reached end of file 2"
                break
        
        f1.close()
        f2.close()
        Of course, iteration will cease when the end of file 1 is reached, or the try/except block will catch the StopIteration exception raised by the next() method. Iteration on the file object in a for loop uses the next() method behind the scenes and catches the StopIteration exception.

        Comment

        • Thebuilderofdoom
          New Member
          • Mar 2010
          • 18

          #5
          um... how come this would work then?

          Code:
          nubr1 = 0
          s = open("C:/DsE2/T1.txt").read()
          f = open("C:/DsE2/T1.txt", 'w')
          
          while 1==1:
          if nubr1 <=5:
          lib1l+`nubr1` = open("C:/DsE2/lib001.txt").readlines()[nubr1]
          lib2l+`nubr1` = open("C:/DsE2/lib002.txt").readlines()[nubr1]
          s = s.replace( lib1l+`nubr1` ,`lib2l+nubr1` )
          nubr1 = nubr1 + 1
          f.write(s)
          else:
          
          f.close()

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            You have no indentation in your code. How will you know what to replace in "T1.txt" if you read the entire file into a string? You cannot make an assignment like this:
            Code:
            lib1l+`nubr1` = ........
            There is no need in reading lib001.txt and lib002.txt over and over again. Read each file once outside the while loop.

            Comment

            • Thebuilderofdoom
              New Member
              • Mar 2010
              • 18

              #7
              so how you read once? then replace all?

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                I am unsure what you are trying to accomplish. Could you be more specific, post a sample from the files and what the results are supposed to look like?

                Comment

                • Thebuilderofdoom
                  New Member
                  • Mar 2010
                  • 18

                  #9
                  ok here is what i am trying.. to do
                  Example:
                  lib001
                  Code:
                  hello
                  hi
                  how
                  are
                  you
                  lib002
                  Code:
                  :D
                  hello
                  :)
                  hi
                  o
                  Target File:Before
                  Code:
                  hi how are you?how's it going?hello?
                  Target File:After
                  Code:
                  hello :) o hi?hello's it going?:D
                  --Example--
                  get all lines in lib001 replaces it it with the lines in lib002 in the target file

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    Now I understand what you are trying to do. Read file1 into a string as you did. Open the two data files and use readlines() to create two lists. Remember that the list elements will have newline characters that need to be stripped. Assuming the string is name s1 and the lists are s2List and s3List:
                    Code:
                    for i, word in enumerate(s2List):
                        s1 = s1.replace(word.strip(), s3List[i].strip())

                    Comment

                    • Glenton
                      Recognized Expert Contributor
                      • Nov 2008
                      • 391

                      #11
                      Code:
                      #First create dictionary for replacements:
                      myD={}
                      L1=open("lib001")
                      L2=open("lib002")
                      for l1,l2 in zip(L1,L2):
                          myD[l1.strip()]=l2.strip()
                      L1.close()
                      L2.close()
                      print myD
                      Gives this:
                      {'how': ':)', 'you': 'o', 'hi': 'hello', 'hello': ':D', 'are': 'hi'}

                      Code:
                      #Next get the text you want to replace
                      T1=open("T1.txt","r")  #read only mode
                      mytxt=T1.readline().strip()
                      T1.close()
                      print mytxt
                      Gives this:
                      hi how are you?how's it going?hello?

                      Code:
                      #Thirdly, do replaces on the text
                      for t1 in myD:
                          mytxt=mytxt.replace(t1,myD[t1])
                      print mytxt
                      Gives this:
                      :D :) hi o?:)'s it going?:D?
                      (NOTE: this may not work properly because it might substitute hi for hello, and then hello for :D - this can be fixed by using two lists instead of a dictionary so that you can control the order of substitution)

                      Code:
                      #Finally, write the new text to file
                      T2=open("T2.txt","w")  #write mode
                      T2.write(mytxt)
                      T2.close()
                      This creates a file called T2.txt with :D :) hi o?:)'s it going?:D? as the content. Of course, you could just use T1.txt if you wanted to over-write the original T1.txt file, but during the development phase that's probably not the best plan!

                      Comment

                      • Thebuilderofdoom
                        New Member
                        • Mar 2010
                        • 18

                        #12
                        um.... it breaks after around 100 lines in the lib001 and lib002

                        Comment

                        • Glenton
                          Recognized Expert Contributor
                          • Nov 2008
                          • 391

                          #13
                          It's possible that it would run out of memory at some point, but not after 100 lines. So they must have the same number of lines. And some characters might cause a problem, I guess.

                          "it breaks" is, I'm afraid, simply woefully inadequate. My program wasn't trying to solve it for you, but rather to show you a method for solving it yourself. If you'd like help debugging, please post the issue.

                          Comment

                          • Thebuilderofdoom
                            New Member
                            • Mar 2010
                            • 18

                            #14
                            the lib001 and the lib002 both have 58112 words each(not together)
                            Error
                            <open file 'C:/DsE2/lib001.txt', mode 'r' at 0x010242F0>
                            <open file 'C:/DsE2/lib002.txt', mode 'r' at 0x010242F0>
                            and then when it put
                            L1=open("C:/DsE2/lib001.txt").re ad()
                            L2=open("C:/DsE2/lib002.txt").re ad()
                            it put rubbish into C:/DsE2/T2.txt

                            Comment

                            • bvdet
                              Recognized Expert Specialist
                              • Oct 2006
                              • 2851

                              #15
                              Did Python raise an exception? Why did you execute file method read() on L1 and L2? That was not in Glenton's example nor mine.

                              Comment

                              Working...