html解码(';utf-8';)csv双线(python 3)
我通过API读取CSV转储,下载是一个字符串(见下面的示例),但当我将其解码为CSV文件时,数据行之间会有一个额外的空行 我需要做什么来删除这些额外的行html解码(';utf-8';)csv双线(python 3),python,html,json,python-3.x,csv,Python,Html,Json,Python 3.x,Csv,我通过API读取CSV转储,下载是一个字符串(见下面的示例),但当我将其解码为CSV文件时,数据行之间会有一个额外的空行 我需要做什么来删除这些额外的行 from urllib.request import urlopen import json, ast import datetime import time import LOM_Config import LOM_GetTokenID def CVS_Download(LOMID, LOMDeviceName): global
from urllib.request import urlopen
import json, ast
import datetime
import time
import LOM_Config
import LOM_GetTokenID
def CVS_Download(LOMID, LOMDeviceName):
global dtime
dtime = time.time()
token_string = LOM_GetTokenID.GetTokenID()
tempstring = 'http://XXXXXXXXXXXXXXXX/' + str(LOMID) + '/csv/?token=' + str(token_string) + '×tamp_to=' + str(dtime) + '&length_of_time=31557600'
file = urlopen(tempstring)
html = file.read()
print(html)
html = html.decode('utf-8')
tmpstring = LOMDeviceName + '.csv'
f = open(tmpstring,'a')
f.write(str(html))
f.close
这是HTML转储
b'time,Light Level,Air Pressure,Humidity,Temperature,CO2,Pollution,Sound\r\n2017-04-01 06:55:00+00:00,56.0,1004.52001953125,56.7000007629395,20.7999992370605,0.0,0.0,38.2862205505371\r\n2017-04-01 06:56:00+00:00,142.0,1004.53002929688,56.5999984741211,20.7999992370605,0.0,0.0,37.7092018127441\r\n
CSV文件将变为
time,Light Level,Air Pressure,Humidity,Temperature,CO2,Pollution,Sound
2017-04-01 06:55:00+00:00,56.0,1004.52001953125,56.7000007629395,20.7999992370605,0.0,0.0,38.2862205505371
2017-04-01 06:56:00+00:00,142.0,1004.53002929688,56.5999984741211,20.7999992370605,0.0,0.0,37.7092018127441
2017-04-01 06:57:00+00:00,142.0,1004.57000732422,56.5,20.7000007629395,0.0,34.6334953308105,39.8081016540527
2017-04-01 06:58:00+00:00,132.0,1004.50994873047,56.5,20.7000007629395,0.0,25.9586906433105,33.675178527832
2017-04-01 06:59:00+00:00,132.0,1004.55004882812,56.5,20.7000007629395,0.0,21.750114440918,32.988037109375
bytestring可以独立于操作系统行为来处理文本。尝试在此文件模式下写入文件,例如:
teststring = b'time,Light Level,Air Pressure,Humidity,Temperature,CO2,Pollution,Sound\r\n2017-04-01 06:55:00+00:00,56.0,1004.52001953125,56.7000007629395,20.7999992370605,0.0,0.0,38.2862205505371\r\n2017-04-01 06:56:00+00:00,142.0,1004.53002929688,56.5999984741211,20.7999992370605,0.0,0.0,37.7092018127441\r\n'
f = open("testfile.csv", 'ab') # <- 'b' = bytestring
f.write(teststring) # <- without converting it to str
f.close()
teststring=b'时间、亮度、气压、湿度、温度、二氧化碳、污染、声音\r\n2017-04-01 06:55:00+00:00,56.01004.52001953125,56.700007629395,20.799992370605,0.0,0.0,38.286220505371\r\n2017-04-01 06:56:00+00:00142.01004.53002929688,56.5999984741211,20.799992370605,0.0,0.0.27431\r\r\n
f=open(“testfile.csv”,“ab”)#@BrendonShaw如果有效,欢迎接受我的答案。这将帮助其他人看到这是解决您问题的方法。