写入多个站点的CSV文件

huangapple go评论101阅读模式
英文:

How to write csv from multiple stations

问题

这是您提供的代码翻译:

# 我为工作项目编写了一个错误记录器。最初我使用了pandas数据框。它运行得还行,但性能有待提高,文件大小也可以改进。我被建议使用CSV写入器。这样我还可以使用锁文件,因为我要在40个站点上使用此程序来写入单个文件。我的问题是,在使用pyinstaller打包后,我可以在所需的站点上进行测试时,出现了一个错误,指出问题在第53行TypeError:write()参数必须为str,而不是list。以下是我的代码。

import os
import csv
import time
import tkinter as tk  # tkinter用于GUI窗口和按钮等。
import datetime  # datetime用于计时器
import configparser

config = configparser.ConfigParser()
config.read("C:/Users/Public/Documents/Intel/Strata/CfgFiles/Stationdata.ini")  # 用于PD数据框的Locationdata。

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_list = []  # PD数据框的时间列表
stat = []  # PD数据框的错误列表。

global currenttime
currenttime = datetime.datetime.today()
window = tk.Tk()  # 将窗口命名为window
window.title(value)  # 在这里,我将窗口的标题设置为工具的位置
window.geometry("250x80")  # 在这里,我们设置窗口的大小

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)

# 如果文件存在,我将尝试在初始文件达到一定行数后创建一个新的日志文件

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():  # 此程序允许我存储时间,并存储用户记录的错误。
    global startzeit
    global time_list
    global stat
    startzeit = datetime.datetime.today()  # startzeit用于通过将时间列表附加到我们的时间列表来记录按钮按下的时间
    time_list.append(startzeit)
    label1.config(text="错误已记录")  # 一旦按下按钮,此消息会短暂显示
    Error = entry1.get()  # 此命令获取用户输入的文本,以便我们可以使用错误列表附加错误
    stat.append(Error)
    location.append(value)
    strata_version.append(version)

label1 = tk.Label(text='currenttime', padx=10, pady=5)  # 创建计时器的大小和位置
label1.place(x=10, y=5, width=200, height=20)

label2 = tk.Label(text="Error:", padx=10, pady=5)  # 创建文本输入框的大小和位置
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="记录错误", command=log)  # 创建一个运行记录程序的按钮
button.place(x=70, y=45, width=70, height=20)

def timer():  # 此程序允许我具有运行带有日期和时间的计时器。
    global currenttime
    currenttime = datetime.datetime.today()
    window.after(1000, timer)  # 此行将计时器设置为1000毫秒后更改一次

def update_label(label1):  # 此程序允许第一个标签每秒更新一次
    new_text = currenttime
    label1.configure(text=new_text)
    label1.after(1000, update_label, label1)

update_label(label1)
timer()  # 再次调用我们的计时器以"运行"
window.mainloop()
英文:

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

答案1

得分: 0

你的错误在这里:

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

你尝试写入一个列表而不是一个字符串到 "Error Log.txt"。将这行改为

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

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

huangapple
  • 本文由 发表于 2023年4月7日 00:24:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951711.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定