写入CSV时跳过行

时间:2018-05-23 20:26:33

标签: python csv

我编写了一个脚本,从两个单独的API中收集数据,但我无法将其显示在一行中,而是显示如下:

Device  Status  Model   StatusSub  

d8d     Online      
122  Disconnected   

当我希望它看起来像这样;

Device  Status  Model   StatusSub

d8d    Online    1202   Disconnected        

这是我的代码,API受密码保护,因此您将无法直接运行它:

while True:#always run
if datetime.datetime.now().minute % 15 == 0 or True:#once every 15 minutes
    name = [('Device'), ('Status'), ('Model'), ('StatusSub')]
    space = [(''),('')]
    print (datetime.datetime.now())
    today = datetime.date.today().strftime('E.Subdevice.Logs.%m-%d-%Y')#file name
    if os.path.exists(today + '.csv'):#Create new file if its a new day
        Append_write = 'ab'
    else:
        Append_write = 'wb'

    with open(today + '.csv',Append_write) as myFile:#open todays csv file, set to write. 
        writer = csv.writer(myFile)
        writer.writerow(name)
        writer.writerow(space)
        #Loop to collect device name and status 500 at a time and print all 2080 in the file specified above.     
        for x in range(5):
            string1 = "https://api.raincloud.com/rest/device?networkName=Company&take=500&skip="+str(x*500)
            response = requests.get(string1,headers=headers).json()
            for y in range (len(response)):
                myData = [(response[y]['device']),(response[y]['status'])]
                string2= 'https://api.raincloud.com/rest/device/'+str(myData[0])+'/subdevice'
                response2 = requests.get(string2,headers=headers).json()
                for z in range (len(response2)):
                     myData2 = [(response2[z]['model']),(response2[z]['status']),datetime.datetime.now()]


                writer.writerow(myData)
                writer.writerow(myData2) 

感谢任何帮助。

0 个答案:

没有答案