How to read a csv file from a python script?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • somialis
    New Member
    • Mar 2007
    • 32

    #16
    Originally posted by ghostdog74
    it does work
    Code:
    import sys
    choice = sys.argv[1]
    for line in open("file"):
         line = line.strip().split()
         if choice == line[0]:
              print line[1:][0].split(";")
    output :
    Code:
    # ./test.py 23
    ['11', '12', '']
    Note that your csv file is now colon separated and not comma separated. So you have to change the split() statement.
    it dint work even now. Let me tell u wht i did:

    1) created a file new.py
    2) Wrote the following code:
    Code:
    import os
    import sys
    import csv
    
    def main(argv):
    
        choice = sys.argv[1]
        fp = open("c:\pst\pst_modified\log_4_0.csv",'r')
        for line in fp:
         line = line.strip().split()
         if choice == line[0]:
              print line[1:][0].split(";")
    3)i have python23 installed at my system
    4) opened the command prompt
    5)Moved to the directory where new.py is placed
    6)executed the following command
    python new.py '23'

    whr am i wrong??
    ./new.py flasgged an error tht "." is not a valid command.

    Comment

    • ghostdog74
      Recognized Expert Contributor
      • Apr 2006
      • 511

      #17
      Originally posted by somialis
      it dint work even now. Let me tell u wht i did:

      1) created a file new.py
      2) Wrote the following code:
      Code:
      import os
      import sys
      import csv
      
      def main(argv):
      
          choice = sys.argv[1]
          fp = open("c:\pst\pst_modified\log_4_0.csv",'r')
          for line in fp:
           line = line.strip().split()
           if choice == line[0]:
                print line[1:][0].split(";")
      3)i have python23 installed at my system
      4) opened the command prompt
      5)Moved to the directory where new.py is placed
      6)executed the following command
      python new.py '23'

      whr am i wrong??
      ./new.py flasgged an error tht "." is not a valid command.

      try this:

      Code:
      import os
      import sys
      import csv ## you are not using this?
      
      filepath = os.path.join("C:\\","pst","pst_modified","log_4_0.csv")
      
      def main():
          choice = sys.argv[1]    
          for line in open(filepath):
           line = line.strip().split()
           if choice == line[0]:
                print line[1:][0].split(";")
      
      main()  ## < --you did not call the main in your last posted script sample.

      Comment

      • somialis
        New Member
        • Mar 2007
        • 32

        #18
        Originally posted by ghostdog74
        try this:

        Code:
        import os
        import sys
        import csv ## you are not using this?
        
        filepath = os.path.join("C:\\","pst","pst_modified","log_4_0.csv")
        
        def main():
            choice = sys.argv[1]    
            for line in open(filepath):
             line = line.strip().split()
             if choice == line[0]:
                  print line[1:][0].split(";")
        
        main()  ## < --you did not call the main in your last posted script sample.

        Hi,
        it's still not working....thr' s something tht i wud like to tell.
        If u put a " print line" statement after
        line = line.strip().sp lit()
        u'll c that it prints the entire csv file and reaches the end. After tht when u compare choice with line[0] ...the condition does not satisfy becauseline[0] contains the last line of the csv file.

        Comment

        • ghostdog74
          Recognized Expert Contributor
          • Apr 2006
          • 511

          #19
          Originally posted by somialis
          Hi,
          it's still not working....thr' s something tht i wud like to tell.
          If u put a " print line" statement after
          line = line.strip().sp lit()
          u'll c that it prints the entire csv file and reaches the end. After tht when u compare choice with line[0] ...the condition does not satisfy becauseline[0] contains the last line of the csv file.
          please try this debugging method:
          Code:
          def main():
              choice = sys.argv[1]    
              for line in open(filepath):
               line = line.strip().split()
               print line, type(line), line[0], line[1]
               raw_input("Press to continue: " )
               if choice == line[0]:
                    print line[1:][0].split(";")
          main()
          please show the output of the above.

          Comment

          • somialis
            New Member
            • Mar 2007
            • 32

            #20
            Originally posted by ghostdog74
            please try this debugging method:
            Code:
            def main():
                choice = sys.argv[1]    
                for line in open(filepath):
                 line = line.strip().split()
                 print line, type(line), line[0], line[1]
                 raw_input("Press to continue: " )
                 if choice == line[0]:
                      print line[1:][0].split(";")
            main()
            please show the output of the above.

            The output of your code is as follows:

            Code:
            [',List', 'Of', 'Individual', 'Patches'] <type 'list'> ,List Of
            Press to continue:
            ['1,00;01;'] <type 'list'> 1,00;01;
            Traceback (most recent call last):
              File "<stdin>", line 1, in ?
              File "my.py", line 12, in main
                print line, type(line), line[0], line[1]
            IndexError: list index out of range

            Comment

            • ghostdog74
              Recognized Expert Contributor
              • Apr 2006
              • 511

              #21
              Originally posted by somialis
              The output of your code is as follows:

              Code:
              [',List', 'Of', 'Individual', 'Patches'] <type 'list'> ,List Of
              Press to continue:
              ['1,00;01;'] <type 'list'> 1,00;01;
              Traceback (most recent call last):
                File "<stdin>", line 1, in ?
                File "my.py", line 12, in main
                  print line, type(line), line[0], line[1]
              IndexError: list index out of range
              hey...you original sample is like this:
              Code:
              List Of Individual Patches
              1 00;01;
              2 00;
              3 01;
              4 02;
              5 00;01;02;
              6 01;02;
              7 01;02;03;04;05;
              8 01;02;03;04;05;06;
              9 00;01;02;03;05;
              10 01;02;03;05;
              11 02;03;05;06;
              12 04;
              13 01;02;03;05;08;09;
              14 00;01;02;03;05;09;
              15 00;01;02;03;05;09;10;
              16 01;02;03;05;08;09;10;
              17 01;02;03;05;08;10;
              18 08;
              19 00;01;02;03;05;08;09;10;
              20 02;03;05;06;08;10;
              21 11;
              22 12;
              23 11;12;
              24 00;01;02;03;05;14;
              so if you have changed your format to like this:
              Code:
              1,00;01;
              2,00;
              ...
              ..
              then surely it doesn't work. you have to modify the code. i give you a hint on where to change:
              Code:
              ....
                  for line in open(filepath):
                   line = line.strip().split()  <---here.
              ....

              Comment

              • somialis
                New Member
                • Mar 2007
                • 32

                #22
                Originally posted by ghostdog74
                hey...you original sample is like this:
                Code:
                List Of Individual Patches
                1 00;01;
                2 00;
                3 01;
                4 02;
                5 00;01;02;
                6 01;02;
                7 01;02;03;04;05;
                8 01;02;03;04;05;06;
                9 00;01;02;03;05;
                10 01;02;03;05;
                11 02;03;05;06;
                12 04;
                13 01;02;03;05;08;09;
                14 00;01;02;03;05;09;
                15 00;01;02;03;05;09;10;
                16 01;02;03;05;08;09;10;
                17 01;02;03;05;08;10;
                18 08;
                19 00;01;02;03;05;08;09;10;
                20 02;03;05;06;08;10;
                21 11;
                22 12;
                23 11;12;
                24 00;01;02;03;05;14;
                so if you have changed your format to like this:
                Code:
                1,00;01;
                2,00;
                ...
                ..
                then surely it doesn't work. you have to modify the code. i give you a hint on where to change:
                Code:
                ....
                    for line in open(filepath):
                     line = line.strip().split()  <---here.
                ....
                i haven't changed the format. My csv file is the same as it was before.It's like this only:

                List Of Individual Patches
                1 00;01;
                2 00;
                3 01;
                4 02;
                5 00;01;02;
                6 01;02;
                7 01;02;03;04;05;
                8 01;02;03;04;05; 06;
                9 00;01;02;03;05;
                10 01;02;03;05;
                11 02;03;05;06;
                12 04;
                13 01;02;03;05;08; 09;
                14 00;01;02;03;05; 09;
                15 00;01;02;03;05; 09;10;
                16 01;02;03;05;08; 09;10;
                17 01;02;03;05;08; 10;
                18 08;
                19 00;01;02;03;05; 08;09;10;
                20 02;03;05;06;08; 10;
                21 11;
                22 12;
                23 11;12;
                24 00;01;02;03;05; 14;
                25 00;01;02;03;05; 09;14;
                26 00;01;02;03;05; 08;09;10;14;
                27 00;01;02;03;05; 17;
                28 00;01;02;03;05; 09;17;
                29 00;01;02;03;05; 08;09;10;17;
                30 01;02;03;05;08; 09;10;17;
                31 00;01;02;03;05; 09;18;
                32 01;02;03;05;08; 09;10;18;

                and after executing ur code snippet i got the error message which i posted in the previous post. Dunno in the output where from the comma is coming.

                Comment

                • ghostdog74
                  Recognized Expert Contributor
                  • Apr 2006
                  • 511

                  #23
                  look at this error you had previously:
                  Code:
                  [',List', 'Of', 'Individual', 'Patches'] <type 'list'> ,List Of
                  Press to continue:
                  ['1,00;01;'] <type 'list'> 1,00;01;
                  Traceback (most recent call last):
                    File "<stdin>", line 1, in ?
                    File "my.py", line 12, in main
                      print line, type(line), line[0], line[1]
                  IndexError: list index out of range
                  the follwing output line results from "print line, type(line), line[0], line[1]" statement:
                  Code:
                  ...
                  ['1,00;01;'] <type 'list'> 1,00;01;
                  ...
                  The "line" value is ['1,00;01;'] after the split. This means it did not split at all. Furthermore, line.strip().sp lit() splits on blanks and since your data have a comma now, it does nothing.
                  notice the "," after the first "1" ?? that's why i deduced that you csv format is different. If otherwise, then i have no idea at all.
                  Code:
                  ...
                  line = line.strip().split(",")
                  ...

                  Comment

                  • somialis
                    New Member
                    • Mar 2007
                    • 32

                    #24
                    Originally posted by ghostdog74
                    look at this error you had previously:
                    Code:
                    [',List', 'Of', 'Individual', 'Patches'] <type 'list'> ,List Of
                    Press to continue:
                    ['1,00;01;'] <type 'list'> 1,00;01;
                    Traceback (most recent call last):
                      File "<stdin>", line 1, in ?
                      File "my.py", line 12, in main
                        print line, type(line), line[0], line[1]
                    IndexError: list index out of range
                    the follwing output line results from "print line, type(line), line[0], line[1]" statement:
                    Code:
                    ...
                    ['1,00;01;'] <type 'list'> 1,00;01;
                    ...
                    The "line" value is ['1,00;01;'] after the split. This means it did not split at all. Furthermore, line.strip().sp lit() splits on blanks and since your data have a comma now, it does nothing.
                    notice the "," after the first "1" ?? that's why i deduced that you csv format is different. If otherwise, then i have no idea at all.
                    Code:
                    ...
                    line = line.strip().split(",")
                    ...
                    hey it worked this time.
                    thanks a ton!! :)

                    Comment

                    • ghostdog74
                      Recognized Expert Contributor
                      • Apr 2006
                      • 511

                      #25
                      Originally posted by somialis
                      hey it worked this time.
                      thanks a ton!! :)
                      so it's your csv format after all?

                      Comment

                      • somialis
                        New Member
                        • Mar 2007
                        • 32

                        #26
                        Originally posted by ghostdog74
                        so it's your csv format after all?
                        dint get ur question.
                        I just included this line

                        Code:
                        line = line.strip().split(",")
                        in place of this
                        Code:
                        line = line.strip().split()

                        Comment

                        • ghostdog74
                          Recognized Expert Contributor
                          • Apr 2006
                          • 511

                          #27
                          Originally posted by somialis
                          dint get ur question.
                          I just included this line

                          Code:
                          line = line.strip().split(",")
                          in place of this
                          Code:
                          line = line.strip().split()
                          if by doing a split on comma and it works, it means your csv file has commas, right? so that's why i ask is your csv format like this instead:
                          Code:
                          1,00;01;
                          2,00;
                          ....
                          ...
                          if its not, then a split on comma will not work.

                          Comment

                          • somialis
                            New Member
                            • Mar 2007
                            • 32

                            #28
                            Originally posted by ghostdog74
                            if by doing a split on comma and it works, it means your csv file has commas, right? so that's why i ask is your csv format like this instead:
                            Code:
                            1,00;01;
                            2,00;
                            ....
                            ...
                            if its not, then a split on comma will not work.
                            ok...now i understood!!
                            Thanks a lot!!

                            Comment

                            Working...