html解码('utf-8')csv双行(python 3)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html解码('utf-8')csv双行(python 3)相关的知识,希望对你有一定的参考价值。
我正在通过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 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
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
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
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
'
f = open("testfile.csv", 'ab') # <- 'b' = bytestring
f.write(teststring) # <- without converting it to str
f.close()
以上是关于html解码('utf-8')csv双行(python 3)的主要内容,如果未能解决你的问题,请参考以下文章
python中sys.setdefaultencoding('utf-8')的作用
python中sys.setdefaultencoding('utf-8')的作用
UnicodeDecodeError:'utf-8'编解码器无法解码位置1中的字节0x8b:无效的起始字节
'utf-8'编解码器无法解码位置8的字节0xfc:无效的起始字节