【发布时间】:2015-10-15 10:18:01
【问题描述】:
我有来自 http 请求的输出,它是字符串类型,但数据类似于 csv。因为我的请求标头中的输出类型是 csv ('Accept':"application/csv")。由于这是源支持的格式。但响应内容类型是字符串。
res=request.contenttype(res)` 给了我字符串。
这是对象(res)的示例输出:
QueryTime
start,end
144488,144490
Data
Data - AData
id,G_id,name,type,time,sid,channel
23,-1,"B1",type1,144488,11,CH23
23,-1,"B1",type1,144488,11,CH23
Data - BData
id,G_id,time,se
23,-1,144488,undefined
23,-1,144488,undefined
如果您看到数据是 csv 格式并且有多个表格,例如您看到“AData”和“BData” 我不知道采取哪种方法来阅读本文。我试过 csv 模块但没有帮助。 我尝试过 dict.csv 进行转换,但同样如此。没有得到想要的输出。 可能是我做错了什么,因为我是 python 新手。 需要的是从输出对象中读取每个表。
with open('file.csv', 'wb') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',',quoting=csv.QUOTE_NONE)
spamwriter.writerow(rec)
with open('file.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print row
请高手指导:-)
【问题讨论】:
-
writerow(rec)不正确(rec不是单个 csv 行)。使用with open('file.csv', 'wb') as file: copyfileobj(r.raw, file)to save the file if you passstream=Truetorequests。
标签: python python-2.7 csv