Support Forums

Full Version: Seprating coma and write in to CSV file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear all,
I have code as below. Which read data from serial port . I am trying to write these data into CSV file. please help me complete this code. I tried on py scripter it worked but code not working


code syntax
Code:
import serial
import csv
import string
import os
import time
import sys

def main():
    pass

if __name__ == '__main__':
    main()
    count=0
    f=open("test.csv","w+");
    result = csv.writer(f,delimiter=',', dialect='excel-tab')

    result_statememt=("date","time","Zenith","Azimuth","Elevation","conv_elevation");
    result.writerow(result_statememt)
    f.close()
    while(count<5):
        #time.sleep(60)
        ser=serial.Serial()
        ser.port=2
        ser.baudrate=9600
        ser.open()
        str=ser.read(109)
        print str
        val=str.split(":")
        print val
        print "\n"
        lines=str.split("\r\n")
        print lines
        #k=lines.split('')
        #print k
        l#ine=lines.split
        wst=[]

        #print line
        wst=[]
      for line in lines[:]:
             line=lines.split(":") [b]/*getting error saying list object has no attribute called split*/[/b]
              print line

        f=open("test.csv","a+")
        result=csv.writer(f,delimiter=',')

        count=count+1



        #lines=str.split("\r\n")

        #print count
        #f=open("test.txt","a+")
        #result=csv.writer(f,delimiter=',')
        #result.writerow()
        f.close()

    f.close()
    ser.close()


output file:

Code:
date is:7/12/16
time is:24-0-0
Zenith:104.85
Azimuth:110.40
Elevation:-14.85
Converted Elevation:15.79

['date is', '7/12/16\r\ntime is', '24-0-0\r\nZenith', '104.85\r\nAzimuth', '110.40\r\nElevation', '-14.85\r\nConverted Elevation', '15.79\r\n']


['date is:7/12/16', 'time is:24-0-0', 'Zenith:104.85', 'Azimuth:110.40', 'Elevation:-14.85', 'Converted Elevation:15.79', '']
Traceback (most recent call last):
  File "C:\Documents and Settings\Administrator\My Documents\python code\code1\module1.py", line 40, in <module>
    line=lines.split
AttributeError: 'list' object has no attribute 'split'
Can I suggest that you visit HackForums.net? People will have more chance helping you over there.