如何从多个站写csv

kmbjn2e3  于 2023-04-09  发布在  其他
关注(0)|答案(1)|浏览(112)

我一直在为一个工作项目写一个错误记录器。我最初使用的是pandas dataframe。它工作正常,但性能还有很多需要改进的地方,文件大小也可以改进。我被推荐使用csv writer。这样我也可以使用lockfile,因为我在40个站点上使用这个程序试图写入单个文件。我的问题是在我使用pyinstaller打包这个,这样我就可以在我想要的站上测试它,我得到一个错误,说明问题是第53行TypeError:write()参数必须是str,而不是list。下面是我的代码。

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()
wsxa1bj1

wsxa1bj11#

我认为你的错误在这里:

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

您尝试将一个列表而不是一个str写入“Error Log.txt”。

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

相关问题