CSV文件写入错误。蟒蛇

时间:2013-12-11 06:06:33

标签: python python-2.7 python-3.x

我之前粘贴了代码。

import serial
import csv
import os
import time
def main():
   pass

if __name__ == '__main__':
    main()
    COUNT=0
    ser=serial.Serial()
    ser.port=2
    ser.baudrate=9600
    foo=open("new.csv","ab");
    result=csv.writer(foo,delimiter=',')
    result_statement=("date","time","Zenith","Azimuth","Elevation","conv_elevation")
    result.writerow(result_statement)
    foo.close()
    while(COUNT<500):
      ser.open()
      str=ser.read(110)
      val=str.split(":")
      print "value is",val
      lines=str.split("\r\n")
      wst=[]
      for line in lines[:-1]:

            parts=line.split(":")
                #print parts
            for p in parts[1:]:
                    wst.append(p)
            #print "wst is", wst
      foo=open("new.csv","a+");
      result=csv.writer(foo,delimiter=',')
      result_statement=wst
      result.writerow(result_statement)
      COUNT=COUNT+1
      #print COUNT
      foo.close()
      ser.close()

这里我得到的输出正确如下。

value is ['date is', ' 1/1/14\r\ntime_is', '9-15-0\r\nZenith', '52.53\r\nAzimuth', '226.85\r\nElevation', '37.47\r\nConverted Elevation', '-46.42\r\nlo']
value is ['date is', ' 1/1/14\r\ntime_is', '9-30-0\r\nZenith', '55.25\r\nAzimuth', '229.47\r\nElevation', '34.75\r\nConverted Elevation', '-42.39\r\nlo']
value is ['date is', ' 1/1/14\r\ntime_is', '9-45-0\r\nZenith', '58.08\r\nAzimuth', '231.84\r\nElevation', '31.92\r\nConverted Elevation', '-38.39\r\nlo']
value is ['date is', ' 1/1/14\r\ntime_is', '10-0-0\r\nZenith', '60.99\r\nAzimuth', '233.98\r\nElevation', '29.01\r\nConverted Elevation', '-34.43\r\nlo']
value is ['date is', ' 1/1/14\r\ntime_is', '10-15-0\r\nZenith', '63.98\r\nAzimuth', '235.92\r\nElevation', '26.02\r\nConverted Elevation', '-30.51\r\nl']
value is ['date is', ' 1/1/14\r\ntime_is', '10-30-0\r\nZenith', '67.03\r\nAzimuth', '237.68\r\nElevation', '22.97\r\nConverted Elevation', '-26.63\r\nl']
value is ['date is', ' 1/1/14\r\ntime_is', '10-45-0\r\nZenith', '70.15\r\nAzimuth', '239.28\r\nElevation', '19.85\r\nConverted Elevation', '-22.78\r\nl']
value is ['date is', ' 1/1/14\r\ntime_is', '11-0-0\r\nZenith', '73.31\r\nAzimuth', '240.73\r\nElevation', '16.69\r\nConverted Elevation', '-18.97\r\nlo']
value is ['date is', ' 1/1/14\r\ntime_is', '11-15-0\r\nZenith', '76.52\r\nAzimuth', '242.05\r\nElevation', '13.48\r\nConverted Elevation', '-15.19\r\nl']
value is ['date is', ' 1/1/14\r\ntime_is', '11-30-0\r\nZenith', '79.76\r\nAzimuth', '243.25\r\nElevation', '10.24\r\nConverted Elevation', '-11.44\r\nl']
value is ['date is', ' 1/1/14\r\ntime_is', '11-45-0\r\nZenith', '83.04\r\nAzimuth', '244.34\r\nElevation', '6.96\r\nConverted Elevation', '-7.72\r\nlon']
value is ['date is', ' 1/1/14\r\ntime_is', '12-0-0\r\nZenith', '86.34\r\nAzimuth', '245.33\r\nElevation', '3.66\r\nConverted Elevation', '-4.02\r\nlong']
value is ['date is', ' 1/1/14\r\ntime_is', '12-15-0\r\nZenith', '89.67\r\nAzimuth', '246.23\r\nElevation', '0.33\r\nConverted Elevation', '-0.36\r\nlon']
value is ['date is', ' 1/1/14\r\ntime_is', '12-30-0\r\nZenith', '93.03\r\nAzimuth', '247.04\r\nElevation', '-3.03\r\nConverted Elevation', '-3.29\r\nlo']
value is ['date is', ' 1/1/14\r\ntime_is', '12-45-0\r\nZenith', '96.40\r\nAzimuth', '247.77\r\nElevation', '-6.40\r\nConverted Elevation', '-6.91\r\nlo']

但是当写入CSV文件时,它正在添加2000个时间cloumn,请让我知道它为什么这样做。

日期时间天顶方位角高度conv_elevation  1/1/14 9/15/2000 52.53 226.85 37.47 -46.42

1/1/14 9/30/2000 55.25 229.47 34.75 -42.39

1/1/14 9-45-0 58.08 231.84 31.92 -38.39

1/1/14 10-0-0 60.99 233.98 29.01 -34.43

1/1/14 10/15/2000 63.98 235.92 26.02 -30.51

1/1/14 10/30/2000 67.03 237.68 22.97 -26.63

1/1/14 10-45-0 70.15 239.28 19.85 -22.78

1/1/14 11-0-0 73.31 240.73 16.69 -18.97

1/1/14 11/15/2000 76.52 242.05 13.48 -15.19

1/1/14 11/30/2000 79.76 243.25 10.24 -11.44

1 个答案:

答案 0 :(得分:0)

转到csv文件选择日期和时间列,并将列格式化为日期和时间。