Reading and writing a text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • psbasha
    Contributor
    • Feb 2007
    • 440

    Reading and writing a text file

    Hi,

    Is it necessary in Python to close the File after reading or writing the data to file?.While refering to Python material ,I saw some where mentioning that no need to close the file.Correct me if I am wrong.

    If possible could anybody help me with sample code for reading and writing a simple text file.I have seen there are many ways to read /write the data in Python.But I want to use the effective way of reading or writing the data from and to file.

    Thanks in advance
    PSB
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by psbasha
    Hi,

    Is it necessary in Python to close the File after reading or writing the data to file?.While refering to Python material ,I saw some where mentioning that no need to close the file.Correct me if I am wrong.

    If possible could anybody help me with sample code for reading and writing a simple text file.I have seen there are many ways to read /write the data in Python.But I want to use the effective way of reading or writing the data from and to file.

    Thanks in advance
    PSB
    This thread shows how to read and write data:http://www.thescripts.com/forum/thre...7166-1-10.html
    There are several other theads on file I/O that I have participated in.

    Python will close an open file in its garbage collection routine when the file object reference is reassigned or decreases to None. It is good practice to close every file that is opened - especially when a file object was created. This brings up a subject that has puzzled me. Open a file like this:
    Code:
    lineLst = open('file_name').readlines()
    Does Python close the file? No file object is created, so the file is closed when the end of file is reached (I think).

    Comment

    • ghostdog74
      Recognized Expert Contributor
      • Apr 2006
      • 511

      #3
      Originally posted by bvdet
      This brings up a subject that has puzzled me. Open a file like this:
      Code:
      lineLst = open('file_name').readlines()
      Does Python close the file? No file object is created, so the file is closed when the end of file is reached (I think).
      yes Python does close the file when the file object gets garbage collected.

      Comment

      • psbasha
        Contributor
        • Feb 2007
        • 440

        #4
        I have a file data in this format

        Employee # Employee Name Salary Location
        ---------------------------------------------------------------------------------------------
        121111 Sam 10,000 NJ
        121311 Paul 20,000 NY
        111111 Jim 10,000 TX

        The data is in Xls and we are copying manually into text file.After copying into text file ,the data is not organized as we see in xls.

        So how to read this file data (without using slicing concept) and store in the respective fields.

        Could anybody provide a sample piece of code.

        Thanks
        PSB

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by psbasha
          I have a file data in this format

          Employee # Employee Name Salary Location
          ---------------------------------------------------------------------------------------------
          121111 Sam 10,000 NJ
          121311 Paul 20,000 NY
          111111 Jim 10,000 TX

          The data is in Xls and we are copying manually into text file.After copying into text file ,the data is not organized as we see in xls.

          So how to read this file data (without using slicing concept) and store in the respective fields.

          Could anybody provide a sample piece of code.

          Thanks
          PSB
          You can save the Excel worksheet as a text file. The text file will be tab delimited which can be easily parsed.
          Code:
          """
          Read a tab delimited file
          """
          
          fn = 'your_file'
          
          f = open(fn, 'r')
          labelLst = f.readline().strip().split('\t')
          lineLst = []
          
          for line in f:
              if not line.startswith('#'):
                  lineLst.append(line.strip().split('\t'))
          
          f.close()
          
          print labelLst
          print lineLst

          Comment

          • psbasha
            Contributor
            • Feb 2007
            • 440

            #6
            Could anybody help me in reading this data.How to seperate the line data and read.

            SET 10 = 1101 1106 1107 1108 1109 1110 1111,
            1112 1113 1114 1115 1116 1117 1118,
            1119 1120 1121 1122 1123 1124 1125

            If anybody provide a sample code for the above it will be helpful.

            Thanks in advance
            PSB

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Originally posted by psbasha
              Could anybody help me in reading this data.How to seperate the line data and read.

              SET 10 = 1101 1106 1107 1108 1109 1110 1111,
              1112 1113 1114 1115 1116 1117 1118,
              1119 1120 1121 1122 1123 1124 1125

              If anybody provide a sample code for the above it will be helpful.

              Thanks in advance
              PSB
              Code:
              s = 'SET 10 = 1101 1106 1107 1108 1109 1110 1111,\n 1112 1113 1114 1115 1116 1117 1118,\n 1119 1120 1121 1122 1123 1124 1125'
              sList = s.split('=')
              label = sList[0].strip()
              data = sList[1].strip().split(',\n')
              datastr = ''.join(data)
              
              print '%s = %s' % (label, datastr)
              '''
              >>> SET 10 = 1101 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
              '''

              Comment

              • psbasha
                Contributor
                • Feb 2007
                • 440

                #8
                Thanks for the reply,

                This is the input file :

                $
                $ SET 10
                $
                $ hjdsahclaladsal kjls
                $PTITLE = SET 10 = SET_110
                SET 10 = 1101 1106 1107 1108 1109 1110 1111,
                1112 1113 1114 1115 1116 1117 1118,
                1119 1120 1121 1122 1123 1124 1125
                $ END OF SET 110
                $

                I have to get the SET # 10 and all the integers starting from the 1101 to 1125.How can I read all the integer numbers from the 1101 to 1125.

                Is it possible with the solution provided by you?.If possible what is the modifications has to be done to that piece of code

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #9
                  Originally posted by psbasha
                  Thanks for the reply,

                  This is the input file :

                  $
                  $ SET 10
                  $
                  $ hjdsahclaladsal kjls
                  $PTITLE = SET 10 = SET_110
                  SET 10 = 1101 1106 1107 1108 1109 1110 1111,
                  1112 1113 1114 1115 1116 1117 1118,
                  1119 1120 1121 1122 1123 1124 1125
                  $ END OF SET 110
                  $

                  I have to get the SET # 10 and all the integers starting from the 1101 to 1125.How can I read all the integer numbers from the 1101 to 1125.

                  Is it possible with the solution provided by you?.If possible what is the modifications has to be done to that piece of code
                  This will give you a list of integers from the data string in my earlier post:
                  Code:
                  map(int, datastr.split())

                  Comment

                  • psbasha
                    Contributor
                    • Feb 2007
                    • 440

                    #10
                    I understand the piece of code what you have posted.But how to capture the data in between the Key Words "SET" and "END".My program should be generic enough to read this data in between this key words

                    SET 10 = 1101 1106 1107 1108 1109 1110 1111,
                    1112 1113 1114 1115 1116 1117 1118,
                    1119 1120 1121 1122 1123 1124 1125
                    $ END OF SET 110

                    Comment

                    • ghostdog74
                      Recognized Expert Contributor
                      • Apr 2006
                      • 511

                      #11
                      Originally posted by psbasha
                      Thanks for the reply,

                      This is the input file :

                      $
                      $ SET 10
                      $
                      $ hjdsahclaladsal kjls
                      $PTITLE = SET 10 = SET_110
                      SET 10 = 1101 1106 1107 1108 1109 1110 1111,
                      1112 1113 1114 1115 1116 1117 1118,
                      1119 1120 1121 1122 1123 1124 1125
                      $ END OF SET 110
                      $

                      I have to get the SET # 10 and all the integers starting from the 1101 to 1125.How can I read all the integer numbers from the 1101 to 1125.

                      Is it possible with the solution provided by you?.If possible what is the modifications has to be done to that piece of code
                      another way

                      Code:
                      >>> import re
                      >>> data = open("file").read()
                      >>> re.compile("SET 10 = (\d+.*)\$ END OF SET 110",re.M|re.DOTALL).findall(data)[0].replace("\n","").replace(","," ")
                      '1101 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125'
                      >>>

                      Comment

                      • bvdet
                        Recognized Expert Specialist
                        • Oct 2006
                        • 2851

                        #12
                        Originally posted by psbasha
                        I understand the piece of code what you have posted.But how to capture the data in between the Key Words "SET" and "END".My program should be generic enough to read this data in between this key words

                        SET 10 = 1101 1106 1107 1108 1109 1110 1111,
                        1112 1113 1114 1115 1116 1117 1118,
                        1119 1120 1121 1122 1123 1124 1125
                        $ END OF SET 110
                        Iterate on the file for line in file: Untested:
                        Code:
                        if line.startswith('SET'):
                            in_set = True
                            s = line
                        elif line.startswith('$'):
                            in_set = False
                        elif in_set:
                            s += line
                        return s

                        Comment

                        • bvdet
                          Recognized Expert Specialist
                          • Oct 2006
                          • 2851

                          #13
                          Originally posted by ghostdog74
                          another way

                          Code:
                          >>> import re
                          >>> data = open("file").read()
                          >>> re.compile("SET 10 = (\d+.*)\$ END OF SET 110",re.M|re.DOTALL).findall(data)[0].replace("\n","").replace(","," ")
                          '1101 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125'
                          >>>
                          I like that ghostdog! :)

                          Comment

                          • psbasha
                            Contributor
                            • Feb 2007
                            • 440

                            #14
                            Originally posted by ghostdog74
                            another way

                            Code:
                            >>> import re
                            >>> data = open("file").read()
                            >>> re.compile("SET 10 = (\d+.*)\$ END OF SET 110",re.M|re.DOTALL).findall(data)[0].replace("\n","").replace(","," ")
                            '1101 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125'
                            >>>
                            What this piece of code will do ?.I am not able to understand what these piece of code will do ?

                            Comment

                            • ghostdog74
                              Recognized Expert Contributor
                              • Apr 2006
                              • 511

                              #15
                              Originally posted by psbasha
                              What this piece of code will do ?.I am not able to understand what these piece of code will do ?
                              I will just briefly explain as my english is not good. hope you will understand
                              The 're' module is regular expression module. More information can be found here at python docs

                              >>> data = open("file").re ad()
                              reads in the whole file as a string to be fed to re module's findall() method

                              >>> re.compile("SET 10 = (\d+.*)\$ END OF SET 110",re.M|re.DO TALL).findall(d ata)[0].replace("\n"," ").replace(",", " ")
                              '1101 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125'
                              >>>
                              you wanted to find the numbers between "SET 10 ="
                              and "$END OF SET 110" . \d+ means find more that one digits. \d+.* means go on and find more of those digits. by putting brackets between (\d+.*), the results of findall() will return these digit groups. re.compile() sets up the pattern that i want to find and re.M means to search for the pattern in multiline mode. re.DOTALL means to make the "." match a newline. In other words, the "." in (\d+.*) will match newline because your numbers are split into multiline. findall() method will do the searching, and will output the results in one list. In this list, there are redundant \n and "," , so got to get rid of them through replace()

                              Anyway , this is just another method. I suggest you use bvdet's method if you are not familiar with regular expression.

                              Comment

                              Working...