Home > Software engineering >  need help removing '[' ']' ' " ' from a csv file
need help removing '[' ']' ' " ' from a csv file

Time:11-28

so i have 2 sections of code one csv writer that read and uploads information from several water quality sensors and uploads it to said csv however it writes into the csv as follows sensor reader and csv writer

import csv
import os
import time
from gdx import gdx 
gdx = gdx.gdx()
gdx.open(connection='usb')   
gdx.select_sensors() 
os.chdir(r"C:\\Users\\")
with open('sensor_data.csv', 'a', newline='') as my_data_file:   
    csv_writer = csv.writer(my_data_file)
    while True:
        gdx.start(period=100) 
        column_headers = gdx.enabled_sensor_info()
        csv_writer.writerow(column_headers)

        for i in range(0,100000):
            measurements = gdx.read()
            localtime = time.localtime()
            result = time.strftime("%I:%M:%S %p", localtime)
            print(result)
            combined_rows = [result, measurements]
            csv_writer.writerow(combined_rows)
            print(measurements)
            my_data_file.flush()
            time.sleep(59.9)

and produces the following csv file

DO Concentration (mg/L),DO Saturation (%),Temperature 
(�C),Pressure (kPa),DO Salinity (mg/L)
11:08:28 PM,"[0.0, 50000.0, 26.399999618530273, 
100.69999694824219, 50.0]"
11:09:28 PM,"[0.0, 50000.0, 26.399999618530273, 
100.69999694824219, 50.0]"
11:10:28 PM,"[0.0, 50000.0, 26.399999618530273, 
100.69999694824219, 50.0]"

however i seem to be unable to remove the double quotation marks and square brackets from the file i also need an additional column for Datetime as you can see, i am extremely new to coding and am way out of my depth any help woudl be appreciated here is the closest i got to solving it

df = pandas.read_csv("sensor_data.csv", encoding="ISO-8859-1")
df = df.replace('\\"','', regex=True)
df = df.replace('\[','', regex=True)
df = df.replace('\]','', regex=True)
df

my failed attempt at removing quotations and square brackets

CodePudding user response:

A simplified version of what you are doing that should work:

import csv
from from datetime import datetime 

column_headers = ["Timestamp","DO Concentration (mg/L)","DO Saturation (%)","Temperature (�C)","Pressure (kPa)","DO Salinity (mg/L)"]

measurements = [[0.0, 50000.0, 26.399999618530273, 100.69999694824219, 50.0], [0.0, 50000.0, 26.399999618530273, 100.69999694824219, 50.0]]

with open('sensor_data.csv', 'w', newline='') as my_data_file:   
    csv_writer = csv.writer(my_data_file)
    csv_writer.writerow(column_headers)
    for row in measurements:
        row_list = row
        row_list.insert(0, datetime.now())
        csv_writer.writerow(row_list)

cat sensor_data.csv 
Timestamp,DO Concentration (mg/L),DO Saturation (%),Temperature (�C),Pressure (kPa),DO Salinity (mg/L)
2022-11-23 11:04:38.843266,0.0,50000.0,26.399999618530273,100.69999694824219,50.0
2022-11-23 11:04:38.843319,0.0,50000.0,26.399999618530273,100.69999694824219,50.0

Realized you did not have to iterate over the values just prepend the timestamp to each measurement row and then write it out.

A modified version of your code. This assumes that measurements in measurements = gdx.read() is a list.

import csv
import os
import time
from gdx import gdx 
gdx = gdx.gdx()
gdx.open(connection='usb')   
gdx.select_sensors() 
os.chdir(r"C:\\Users\\")
with open('sensor_data.csv', 'a', newline='') as my_data_file:   
    csv_writer = csv.writer(my_data_file)
    while True:
        gdx.start(period=100) 
        column_headers = gdx.enabled_sensor_info()
        column_headers.insert(0, "Timestamp")
        csv_writer.writerow(column_headers)

        for i in range(0,100000):
            measurements = gdx.read()
            localtime = time.localtime()
            result = time.strftime("%I:%M:%S %p", localtime)
            print(result)
            combined_rows = measurements.insert(0, result)
            csv_writer.writerow(combined_rows)
            print(combined_rows)
            my_data_file.flush()
            time.sleep(59.9)

  • Related