簡體   English   中英

如何在Tkinter python2.7中將數據幀導出到CSV?

[英]How to export dataframe to CSV in Tkinter python2.7?

我試圖讓用戶添加三個條目,並且代碼將使用它們從API獲取數據,然后將數據導出到CSV。 我正在嘗試這樣做,但是我不確定是什么問題,而且我對Tkinter還是很陌生。

from Tkinter import *
import sys
import csv
from urllib import urlopen
import json
from dateutil.relativedelta import relativedelta
from datetime import datetime
import pandas as pd

root = Tk()
start2 = StringVar()
end2 = StringVar()
root.geometry("1600x800+0+0")
root.title("Data")
startd = "2017-13-03"
endd = "2017-13-03"
power1 =""
ID = "1232.4343.323"
class data1:
    def __init__(self,master):
        frame = Frame(master)
        #frame.pack()
        Tops = Frame(root, width=1600, height=1000, bg="powder blue")
        Tops.pack(side=TOP)
        self.output()

        thelabel = Label(Tops, text="Data",font =('arial',30))
        thelabel.pack(side=TOP)

    def output(self):
        Label(text='Start Date:',font=('arial', 12, 'bold')).pack(side=LEFT,padx=12,pady=12)
        self.startd = Entry(root, width=10)
        self.startd.pack(side=LEFT,padx=2,pady=16)
        Label(text='End Date:',font=('arial', 12, 'bold')).pack(side=LEFT,padx=12,pady=12)
        self.endd = Entry(root, width=10)
        self.endd.pack(side=LEFT,padx=2,pady=16)
        Label(text='Device ID:',font=('arial', 12, 'bold')).pack(side=LEFT,padx=12,pady=12)
        self.ID = Entry(root, width=10)
        self.ID.pack(side=LEFT,padx=2,pady=16)

        self.b = Button(root, text='Submit', command=self.getjsondata)
        self.b.pack(side=LEFT,padx=20,pady=5)

    def getjsondata(self):
        global power1
        url1 = "https://api.data.com/mongo/measures/"+ID+"/"+startd+"/"+endd+"/?format=json"
        power1 = urlopen(url1).read()
        power1 = json.loads(power1)
        power11 = pd.DataFrame(power1)
        power11.csv(power11, file="MyData.csv")



b = data1(root)
root.mainloop()

數據采用json格式,這就是數據的來源:

[{ “時間戳”: “2017-01-14T19:47:00Z”, “值”: “423”},{ “時間戳”: “2017-01-14T19:47:30Z”, “值”:“419 “},{” 時間戳 “:” 2017-01-14T19:48:00Z”, “值”: “431”},{ “時間戳”: “2017-01-14T19:48:30Z”, “值”: “429”},{ “時間戳”: “2017-01-14T19:49:00Z”, “值”: “422”},{ “時間戳”: “2017-01-14T19:49:30Z”,“值“:” 427 “},{” 時間戳 “:” 2017-01-14T19:50:00Z”, “值”: “426”},{ “時間戳”: “2017-01-14T19:50:30Z”, “值”: “427”},{ “時間戳”: “2017-01-14T19:51:00Z”, “值”: “428”},{ “時間戳”:“2017-01-14T19:51:30Z “ ”值“: ”426“},{ ”時間戳“: ”2017-01-14T19:52:00Z“, ”值“: ”424“},{ ”時間戳“:” 2017-01-14T19:52 :30Z “ ”值“: ”423“},{ ”時間戳“: ”2017-01-14T19:53:00Z“, ”值“: ”453“},{ ”時間戳“:” 2017-01-14T19 :53:30Z “ ”值“: ”433“},{ ”時間戳“: ”2017-01-14T19:54:00Z“, ”值“: ”445“},{ ”時間戳“:” 2017-01 -14T19:54:30Z “ ”值“: ”438“},{ ”時間戳“: ”2017-01-14T19:55:00Z“, ”值“: ”430“},{ ”時間戳“:” 2017年-01-14T19:55:30Z”, “值”: “437”},{ “時間戳”: “2017-01-14T19:56:00Z”, “值”: “425”},{ “時間戳”: “2017-01-14T19:56 :30Z “ ”值“: ”420“},{ ”時間戳“: ”2017-01-14T19:57:00Z“, ”值“: ”431“},{ ”時間戳“:” 2017-01-14T19 :57:30Z “ ”值“: ”435“},{ ”時間戳“: ”2017-01-14T19:58:00Z“, ”值“: ”443“},{ ”時間戳“:” 2017-01 -14T19:58:30Z “ ”值“: ”430“},{ ”時間戳“: ”2017-01-14T19:59:00Z“, ”值“: ”425“},{ ”時間戳“:” 2017年-01-14T19:59:30Z”, “值”: “406”},{ “時間戳”: “2017-01-14T20:00:00Z”, “值”: “412”},{ “時間戳”: “2017-01-14T20:00:30Z”, “值”: “417”},{ “時間戳”: “2017-01-14T20:01:00Z”, “值”: “422”},{“時間戳“:” 2017-01-14T20:01:30Z”, “值”: “422”},{ “時間戳”: “2017-01-14T20:02:00Z”, “值”: “418”},{ “時間戳”: “2017-01-14T20:02:30Z”, “值”: “415”},{ “時間戳”: “2017-01-14T20:03:00Z”, “值”: “423”} ,{ “時間戳”: “2017-01-14T20:03:30Z”, “值”: “411”},{ “時間戳”: “2017-01-14T20:04:00Z”, “值”:“413 “}]

我已經使用panadas過濾了數據,主要問題是如何將條目鏈接到可以在代碼中使用的變量。 這些條目是:ID:開始日期:結束日期:

請讓我知道我的代碼中有什么問題。另一方面,如果可能的話,我打算讓用戶打開將csv文件保存到計算機上的位置。

提前致謝

如果問題在pandas方面:

似乎您需要read_json ,然后如果需要通過timestamps選擇數據,請使用loc

df = pd.read_json(url1).set_index('timestamp')
print (df)
                     value
timestamp                 
2017-01-14 19:47:00    423
2017-01-14 19:47:30    419
2017-01-14 19:48:00    431
2017-01-14 19:48:30    429
...
...

date = pd.to_datetime('2017-01-14 19:47:00')
var = df.loc[date, 'value']
print (var)
423

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM