複数のCSVファイルをExcelファイルに結合したいと考えています。
VBAを使ってコードを書いたことはありますが、
学習の為Pythonで実現したいと考えています。
複数サイトを参考に下記のコード作成しましたが、
エラーが出ている状況です。
打開策をご教授頂けないでしょうか。
【コード】
import tkinter as tk
import tkinter.filedialog as fl
import tkinter.messagebox as mb
import numpy as np
import time
import openpyxl as px
root=tk.Tk()
def get(*args):
wb=px.Workbook() sheet_title=["計算結果1","計算結果2","計算結果3"] filetype=[("all file","*")] path=fl.askopenfilenames(initialdir="C:/Users/ユーザー名/Desktop",filetypes=filetype,title="select file") print(path) for i in range(len(path)): data_1, data_2,data_3= np.loadtxt(fname=path[i], skiprows=2, unpack=True) new_sheet=wb.create_sheet("計算結果") ws = wb["計算結果"] ws.title=sheet_title[i] list_target=["物理量A(単位)","物理量B(単位)","物理量C(単位)"] for i in range(len(list_target)): ws.cell(row=1,column=i+1,value=list_target[i]) for i in range(len(data_1)): ws.cell(row=i+2,column=1,value=data_1[i]) ws.cell(row=i+2,column=2,value=data_2[i]) ws.cell(row=i+2,column=3,value=data_3[i]) timestr=time.strftime("%Y%m%d-%H%M%S") wb.save(r"/Users/ユーザー名/Desktop/test"+timestr+".xlsx") mb.showinfo("確認","Excelファイルの作成が完了しました") message["text"]="処理が完了しました"
message=tk.Label(root,text="ファイルを選択してください",width=30)
message.grid(row=0,column=0)
button=tk.Button(text="開く",command=get)
button.grid(row=0,column=1)
root.mainloop()
【エラー内容】
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\ユーザー\Anaconda3\lib\tkinter_init_.py", line 1705, in call
return self.func(*args)
File "<ipython-input-2-2bb2b54bb6c2>", line 27, in get
data_1, data_2,data_3= np.loadtxt(fname=path[i], skiprows=2, unpack=True)
File "C:\Users\ユーザー\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 1141, in loadtxt
for x in read_data(_loadtxt_chunksize):
File "C:\Users\ユーザー\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 1068, in read_data
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "C:\Users\ユーザー\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 1068, in <listcomp>
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "C:\Users\ユーザー\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 775, in floatconv
return float(x)
ValueError: could not convert string to float: '8,8,8,8,8'
回答1件
あなたの回答
tips
プレビュー