add new field(column) to a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mari2025
    New Member
    • Apr 2013
    • 6

    add new field(column) to a file

    Hello,
    I am beginner in Python and I need your guidance about the task I am given. I have to read a laser data file in python and then add two new columns to the data and fill them with specific integer values. I was able to read the file but was not successful in adding the new columns.I would be happy if you can kindly help me with it.
    Code:
    f = file("E:/SAGA/data/325125404.all", "r")
    >>> f.read()
    >>> w = file("E:/SAGA/data/325125404a.all", "w")
    >>> t=[]
    >>> for line in f.readlines():
    	t.append('\n',1)
    	w.writelines(t)
    	w.close()
    Last edited by bvdet; Apr 2 '13, 02:35 PM. Reason: Please use code tags when posting code [code]....[/code]
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Let's assume you want a comma delimited file, and you want to add two columns with the numbers 1 and 2 to each line. This is untested:
    Code:
    data = open("E:/SAGA/data/325125404.all", "r").readlines()
    output = ["%s,%s,%s" % (item.strip(), 1, 2) for item in data]
    f = open("E:/SAGA/data/325125404_modified.all", "w")
    f.write("\n".join(output))
    f.close()
    I would suggest a different file name for the output, at least for testing.

    Comment

    • mari2025
      New Member
      • Apr 2013
      • 6

      #3
      thank you very much for your prompt reply.
      I tried it and works. Moreover,
      if I want to add the new column using space between the new and the others what should I do?

      Comment

      • mari2025
        New Member
        • Apr 2013
        • 6

        #4
        I also have question about "%s,", does not it use for the strings? as the 1 and 2 are numbers, should I use integer?

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by mari2025
          I also have question about "%s,", does not it use for the strings? as the 1 and 2 are numbers, should I use integer?
          %s will type cast the numbers to str.

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            The formatted string would be:
            "%s %s %s" % (item.strip(), 1, 2)

            Comment

            • mari2025
              New Member
              • Apr 2013
              • 6

              #7
              I have changed your code a bit and now it shows what I really want:
              Code:
              data = open("E:/SAGA/data/325125404.all","r").readlines()
              output = ["%s\t%s" %(item.strip(),1) for item in data]
              f = open("E:/SAGA/data/325125404_mod.all","w")
              f.write("\n".join(output))
              f.close()
              but still I am not sure that no where I have introduced the new column type as integer, or maybe I am wrong as I am beginner in Python. I would be thankful if you guide me about it.
              Last edited by Rabbit; Apr 2 '13, 05:48 PM. Reason: Please use code tags when posting code.

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Everything written to a file must be a string. File contents are read as strings and must be type cast if another object type is required.

                Comment

                • mari2025
                  New Member
                  • Apr 2013
                  • 6

                  #9
                  thank you very much for your helpful guidance. I would like to ask another question. I am going to apply the above code for many files those are in one folder. in your view, what is the fastest approach, I was thinking I should create a function or is there any other methods for to do it as quick as possible?

                  Comment

                  Working...