简体   繁体   English

如何将逗号分隔的 object 导出到 a.csv 文件中

[英]How to export comma separated object into a .csv file

The below code is meant to output the raw data from the designated serial port to the python shell as well as the amended data now in CSV format ready to output through csv.writer into a.csv file. The below code is meant to output the raw data from the designated serial port to the python shell as well as the amended data now in CSV format ready to output through csv.writer into a.csv file. However the final output is shown as Fri Apr 23 21:30:05 2021 "19,62,0,0,25. As you may be able to observe this is incorrect as the output should be [time.asctime(time.localtime(time.time())),decoded_bytes] translating to Fri Apr 23 21:30:05 2021, 19,62,0,0,25. I'm not sure how the ' " ' was added nor why python considers the '19' part of the date-time object and places it in the same column.但是,最终的 output 显示为 Fri Apr 23 21:30:05 2021 "19,62,0,0,25。您可能会发现这是不正确的,因为 output 应该是 [time.asctime(time.localtime. (time.time())),decoded_bytes] 转换为 Fri Apr 23 21:30:05 2021, 19,62,0,0,25。我不确定“”是如何添加的,也不知道为什么 python 会考虑日期时间 object 的“19”部分并将其放在同一列中。 Any help would be appreciated.任何帮助,将不胜感激。

import serial
import time
import csv

#ser = serial.Serial('/dev/ttyUSB0', baudrate = 9600, timeout = 1)
ser = serial.Serial('COM4', baudrate = 9600)
ser.flushInput()

while True:
    
    ser_bytes = ser.readline()
    line = ser.readline().decode('utf-8')[:-1]
    decoded_bytes = line.replace(":","",5).replace("Soil Moisture","",1).replace("Humidity","",1).replace("CO2","",1).replace("ppm","",1).replace("ppb","",1).replace("%RH","",1).replace("Temp","",1).replace("EC","",1).replace("TVOC","",1).replace(" ","",8)

    if line:  # If it isn't a blank line
        print("Incoming DATA:")
        print(line)
        print(".csv DATA:")
        print(decoded_bytes)
        with open("test_data.csv","a") as f:
            writer = csv.writer(f,delimiter=" ")
            writer.writerow([time.asctime(time.localtime(time.time())),decoded_bytes])

sample python shell output:样品 python shell output:

Incoming DATA: Soil Moisture: 22EC, Humidity: 62.00%RH, CO2: 0ppm, TVOC: 0ppb, Temp:25.00输入数据:土壤湿度:22EC,湿度:62.00%RH,CO2:0ppm,TVOC:0ppb,温度:25.00

.csv DATA: 22,62.00,0,0,25.00 .csv 数据:22,62.00,0,0,25.00

Incoming DATA: Soil Moisture: 18EC, Humidity: 62.00%RH, CO2: 400ppm, TVOC: 0ppb, Temp:24.45输入数据:土壤湿度:18EC,湿度:62.00%RH,CO2:400ppm,TVOC:0ppb,温度:24.45

.csv DATA: 18,62.00,400,0,24.45 .csv 数据:18,62.00,400,0,24.45

Incoming DATA: Soil Moisture: 15EC, Humidity: 62.00%RH, CO2: 400ppm, TVOC: 0ppb, Temp:25.00输入数据:土壤湿度:15EC,湿度:62.00%RH,CO2:400ppm,TVOC:0ppb,温度:25.00

.csv DATA: 15,62.00,400,0,25.00 .csv 数据:15,62.00,400,0,25.00

Try to adapt this solution, see if it helps.尝试调整此解决方案,看看是否有帮助。

file_input.txt文件输入.txt

Soil Moisture: 22EC, Humidity: 62.00%RH, CO2: 0ppm, TVOC: 0ppb, Temp:25.00
Soil Moisture: 18EC, Humidity: 62.00%RH, CO2: 400ppm, TVOC: 0ppb, Temp:24.45
Soil Moisture: 15EC, Humidity: 62.00%RH, CO2: 400ppm, TVOC: 0ppb, Temp:25.00

script脚本

import time



import re

def write_to_csv(list_input):
    import csv
    with open('file_output.csv', 'a', newline='') as myfile:
        wr = csv.writer(myfile, quoting=csv.QUOTE_NONNUMERIC)
        wr.writerow(list_input)

def read_input(file_input):
    with open(file_input, 'r') as file_in:
        list_file = file_in.read().splitlines()
        for i in list_file:
            # Find the pattern
            res=re.findall(': ?(\d*\.?\d*)', i)
            # Convert the list to number
            res=list(map(float,res))
            time_stamp = time.asctime(time.localtime(time.time()))
            res.insert(0,time_stamp)
            write_to_csv(res)


file_input = 'file_input.txt'
read_input(file_input)

file_output.csv文件输出.csv

"Fri Apr 23 10:29:11 2021",22.0,62.0,0.0,0.0,25.0
"Fri Apr 23 10:29:11 2021",18.0,62.0,400.0,0.0,24.45
"Fri Apr 23 10:29:11 2021",15.0,62.0,400.0,0.0,25.0

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

相关问题 如果列中包含逗号分隔的元素,如何将逗号分隔的 `csv` 文件读入 pandas dataframe? - How to read a comma separated `csv` file into pandas dataframe if it contains a comma separated elements in a column? 如何在 python 的 CSV 文件中写入逗号分隔值? - How to write comma-separated values in a CSV file in python? 如何从CSV文件中分离逗号分隔的数据? - How to separate comma separated data from csv file? 将逗号分隔的字符串导出为文本文件,而不将其自动格式化为 CSV - export a comma-separated string as a text file without auto-formatting it as a CSV 逗号分隔的csv文件中的python dict - python dict in comma separated csv file Scrapy yield逗号分隔的csv文件 - Scrapy yield comma separated csv file CSV 文件中的逗号分隔小数位 - Comma separated decimal places in a CSV file 使用 python 中的引号解析逗号分隔的 csv 文件 - parse comma separated csv file with quotes in python 您如何将带引号,逗号分隔的项目列表写入CSV文件,而引号列表中没有最后一个逗号? - How do you write a quoted, comma separated list of items to CSV file without the last comma in quoted list? 如何将文本文件转换为 csv 文件,第一个括号作为列,后跟每个逗号分隔值的列? - How to convert text file into csv file with first brackets as column followed by columns for each comma separated value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM