Marshmellow Physics
Marshmellow Physics

Reputation: 11

How to write csv from multiple stations

I have been writing an error logger for a work project. I initially was using a pandas dataframe. It was working alright but the performance left much to be wanted and the filesize could have been improved. I was recommended to use the csv writer. This way I could also use lockfile as I am using this program across 40 stations trying to write to a single file. My issue is after I use pyinstaller to pack this so I can test it on my desired station i get an error stating the issue is line 53 TypeError:write() argument must be str, not list. Below is my code.

import os
import csv
import time
import tkinter as tk  # tkinter is used for the gui window and button etc.
import datetime  # datetime is used for our timers
import configparser

config = configparser.ConfigParser()
config.read("C:/Users/Public/Documents/Intel/Strata/CfgFiles/Stationdata.ini")  #Locationdata for PD dataframe.

for key, value in config["StationData"].items():
    print(value)
    
config.read("C:/Users/Public/Documents/Intel/Strata/CfgFiles/Appinfo.ini")

for key, version in config["About"].items():
    print(version)

strata_version = []
location = []
time = []  #time list for PD dataframe
stat = []  #Error list for PD dataframe.
 
global currenttime
currenttime = datetime.datetime.today()
window = tk.Tk()  # naming our window: window
window.title(value)  # here I set the title of the window to the location of the tool
window.geometry("250x80")  # here we set the size of the window

filepath = '//datagrovera.ra.intel.com/DeviceLabUser/Shared/Device lab Error Logger/Error Log.txt'
lockfile = filepath + 'lock'

headers = ['time', 'Error', 'location', 'strata_version']
if not os.path.isfile("//datagrovera.ra.intel.com/DeviceLabUser/Shared/Device lab Error Logger/Error Log.txt"):
    with open("//datagrovera.ra.intel.com/DeviceLabUser/Shared/Device lab Error Logger/Error Log.txt", 'w', newline='') as w:
        writer = csv.writer(w)
        writer.writerow(headers)
        

#if os.path.isfile(//datagrovera.ra.intel.com/DeviceLabUser/Shared/Device lab Error Logger/Error Log.txt): 
#I am going to try to create a new log file once the initial file reaches a certain number of lines
    
while os.path.isfile(lockfile):
    time.sleep(1)

with open('//datagrovera.ra.intel.com/DeviceLabUser/Shared/Device lab Error Logger/Error Log.txt', 'a', newline='') as w:
    w.write([str(1), str(3), str(os.getpid())])

with open(lockfile, 'w') as w:
    w.write(str(os.getpid()))
    
os.remove(lockfile)

def log():  # this program lets me store the time, and store the error logged by the user.
    global startzeit
    global time
    global stat
    startzeit = datetime.datetime.today()  #startzeit is used to log the time of our button press by appending our time list
    time.append(startzeit)
    label1.config(text="Error has been logged")  # once button is pressed this message shows briefly
    Error = entry1.get() #This command pulls the user inputted text so that we can append our stat list with the errors
    stat.append(Error) 
    location.append(value)
    strata_version.append(version)
    
    
label1 = tk.Label(text='currenttime', padx=10, pady=5)  # creating the size and placement for our timer
label1.place(x=10, y=5, width=200, height=20)

label2 = tk.Label(text="Error:", padx=10, pady=5)  # creating the size and placement for text entry box
label2.place(x=7, y=20, width=30, height=20)

entry1 = tk.Entry(window, )
entry1.place(x=40, y=20, width=200, height=20)

button = tk.Button(window, text="Log Error", command=log)  # creating a button that runs the log program
button.place(x=70, y=45, width=70, height=20)


def timer():  # this program allows me to have a timer running with the date and time.
    global currenttime
    currenttime = datetime.datetime.today()
    window.after(1000, timer)  # this line sets the timer to change after 1000 milliseconds


def update_label(label1):  #This program allows the first label to update every second
    new_text = currenttime
    label1.configure(text=new_text)
    label1.after(1000, update_label, label1)

update_label(label1)
timer()  # calling our timer once more to "run"
window.mainloop()

Upvotes: 0

Views: 40

Answers (1)

apapillon
apapillon

Reputation: 116

I think your error is here :

    w.write([str(1), str(3), str(os.getpid())])

You try to write a list and not a str into "Error Log.txt". Change this line to

    w.write(str([str(1), str(3), str(os.getpid())]))

Upvotes: 0

Related Questions