#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    Delhi
    Posts
    35
    Rep Power
    2

    Unhappy Pytho write to excel


    Dear all.

    I have small isuue in writing in to excel sheet via python.When ever write in the excel it leaving one extra row each time. how to remove this row.

    Code:
    import serial
    import csv
    import os
    import time
    import sys
    import string
    from threading import Timer
    
    
    
    def main():
        pass
    
    if __name__ == '__main__':
        main()
        COUNT=0
    
        f=open("test.csv","w+");
        result = csv.writer(f,delimiter=',')
        result_statement=("mode","date","time")
        result.writerow(result_statement)
        f.close()
    
    
        while COUNT<=100:
        #while():
          time.sleep(60)
          ser=serial.Serial()
          ser.port=12
          ser.baudrate=9600
          ser.open()
    
          str=ser.read(150)
          # print "string are:\n",str
          print type(str)
          val=str.split(":")
          # print "value is:\n",val
          lines=str.split("\r\n")
         # print  "line statement are :\n",lines
          COUNT=COUNT+1
          print COUNT
    
    
    
    
          f=open("test.csv","a+");
          result = csv.writer(f,delimiter=',')
          wst=[]
          for line in lines[:-1]:
                parts=line.split(":")
                for p in parts[1:]:
                    wst.append(p)
    
    
                #result = csv.writer(f,delimiter=',')
                #wst.append(parts[1:])
    
          print "wst:\n",wst
    
          result.writerow(wst)
          f.close()
    
    
    
    
               
    f.close()
    ser.close()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Please use code tags, otherwise it's impossible to understand your code.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Could you also post sample data from your output file, test.csv ?

    Comments on this post

    • ajit.nayak87 agrees
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    Delhi
    Posts
    35
    Rep Power
    2
    I didnt fine file upload option here . this data format it look like

    Dir ACTUATOR_ON_OFF MODE DATE TIME TRACKER DESIRED ANGLE TRACKER ACTUAL ANGLE

    FWD 0 1 14/3/2013 11.35.45 2.68 4.89

    FWD 0 1 14/3/2013 11.36.48 2.58 4.89

    FWD 0 1 14/3/2013 11.37.49 2.47 4.89

    FWD 0 1 14/3/2013 11.38.51 2.36 4.89

    FWD 0 1 14/3/2013 11.39.54 2.25 4.89

    FWD 0 1 14/3/2013 11.40.57 2.15 4.89

    FWD 0 1 14/3/2013 11.41.59 2.04 4.89

    FWD 0 1 14/3/2013 11.43.2 1.83 3.92

    FWD 0 1 14/3/2013 11.44.5 1.72 3.92

    FWD 0 1 14/3/2013 11.45.7 1.61 3.92

    FWD 0 1 14/3/2013 11.46.10 1.5 3.92

    FWD 0 1 14/3/2013 11.47.13 1.4 3.92

    FWD 0 1 14/3/2013 11.48.15 1.29 3.92

    FWD 0 1 14/3/2013 11.49.18 1.18 3.92

    FWD 0 1 14/3/2013 11.50.21 1.08 3.92
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by ajit.nayak87
    I didnt fine file upload option here . this data format it look like

    Dir ACTUATOR_ON_OFF MODE DATE TIME TRACKER DESIRED ANGLE TRACKER ACTUAL ANGLE

    FWD 0 1 14/3/2013 11.35.45 2.68 4.89
    [...]
    Is this data copied from excel, or from the text file itself?

    In your code you set the delimiter to comma...
    Code:
    result = csv.writer(f,delimiter=',')
    ...yet in the data above I don't see any commas.

    Please provide output _exactly_ as it is represented in the text file.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    Delhi
    Posts
    35
    Rep Power
    2
    this data already get printing in excel. But i found that whenever it write in to excel it leave one blank row. i wanna know , why it is writing it


    Originally Posted by partoj
    Is this data copied from excel, or from the text file itself?

    In your code you set the delimiter to comma...
    Code:
    result = csv.writer(f,delimiter=',')
    ...yet in the data above I don't see any commas.

    Please provide output _exactly_ as it is represented in the text file.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by ajit.nayak87
    this data already get printing in excel. But i found that whenever it write in to excel it leave one blank row. i wanna know , why it is writing it
    Maybe try using the 'excel-tab' dialect instead:

    Code:
    result = csv.writer(f,delimiter=',', dialect='excel-tab')
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    Delhi
    Posts
    35
    Rep Power
    2
    it didnt worked . printing same




    Originally Posted by partoj
    Maybe try using the 'excel-tab' dialect instead:

    Code:
    result = csv.writer(f,delimiter=',', dialect='excel-tab')
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by ajit.nayak87
    it didnt worked . printing same
    Sounds like an excel issue then, which is beyond the scope of this forum.

IMN logo majestic logo threadwatch logo seochat tools logo