[英]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.