简体   繁体   English

使用python写入CSV文件

[英]Writing into CSV file using python

Here some simple code I have written. 这里有一些我编写的简单代码。 Data is not accumulated in the CSV file; 数据不会累积在CSV文件中; can someone share code to read data from serial port and log this to a CSV file? 有人可以共享代码从串口读取数据并将其记录到CSV文件?

import serial
import csv
import string
import os
import time
import sys

def main():
    pass

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

    result_statememt=("date","zenith","elevation","azimuth","conv_elevation");
    result.writerow(result_statememt)
    f.close()
    while(count<10):
        #time.sleep(60)
        ser=serial.Serial()
        ser.port=3
        ser.baudrate=9600
        ser.open()
        str=ser.read(50)
        val=str.split(":")
        lines=str.split("\r\n")
        count=count+1
        print count
        f=open("test.txt","a+")
        result=csv.writer(f,delimiter=',')
        result.writerow()
        f.close()

    f.close()
    ser.close()

As noted, since you're not passing anything to result.writerow() nothing is being written to the CSV file. 如上所述,由于您没有向result.writerow()传递任何内容, result.writerow()没有任何内容写入CSV文件。 But I'm also concerned that you're reading 50 bytes at a time from the serial port, then splitting it on the : character and then splitting it on a \\r\\n delimiter. 但是我也担心你从串口一次读取50个字节,然后将它拆分为:字符,然后将它拆分为\\r\\n分隔符。 I don't think this is going to do what you want. 我不认为这会做你想要的。

My guess is that you are expecting the serial port to deliver lines terminated by \\r\\n , each line consisting of a set of fields separated by : , and you want to write these fields to a CSV file. 我的猜测是你希望串口传递由\\r\\n终止的行,每行包含一组以:分隔的字段,并且你想将这些字段写入CSV文件。 I'd recommend trying something like this instead: 我建议尝试这样的事情:

with open("test.txt", "wb") as output_file:
    csv_out = csv.writer(output_file, delimiter=',', dialect='excel-tab')
    csv_out.writerow("date","zenith","elevation","azimuth","conv_elevation")

    ser=serial.Serial(port=3, baudrate=9600, timeout=60)
    ser.open()
    for count in range(10):
        str = ser.readline().rstrip()
        csv_out.writerow(str.split(':'))
    ser.close()

I haven't used the Python serial module and don't currently have hardware to test this, but based on my reading of the module documentation this should be closer to what you want to do. 我没有使用Python serial模块,并且目前没有硬件来测试它,但根据我对模块文档的阅读,这应该更接近你想要做的事情。 I think the read timeout is measured in seconds, so this will time out if the serial port doesn't produce any data for one minute. 认为读取超时是以秒为单位测量的,因此如果串口在一分钟内没有产生任何数据,这将超时。

import serial
import csv
import string
import os
import time
import sys

def main():
    pass

if __name__ == '__main__':
    main()
    ser=serial.Serial()
    ser.port=2
    print ser.port
    ser.baudrate=9600
    with open("test.csv", "wb") as output_file:
        csv_out = csv.writer(output_file, delimiter=',', dialect='excel-tab')
        C_statememt=("date","time","Zenith","Azimuth","Elevation","conv_elevation");
    ser.open()
    for count in range(10):
        str = ser.readline().rstrip()
        C_statememt.writerow(str.split(':'))
    ser.close()

error report: 错误报告:

2
Traceback (most recent call last):
  File "C:\Documents and Settings\Administrator\My Documents\python code\code3\module1.py", line 28, in <module>
    C_statememt.writerow(str.split(':'))
AttributeError: 'tuple' object has no attribute 'writerow'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM