【问题标题】:Can I use Python's CSV reader with Google Fusion Tables?我可以将 Python 的 CSV 阅读器与 Google Fusion Tables 一起使用吗?
【发布时间】:2011-10-17 22:12:41
【问题描述】:

我正在尝试使用 csv 库将来自 Google Fusion Tables API 的数据读入 Python。似乎查询 API 会返回 CSV 数据,但是当我尝试将它与 csv.reader 一起使用时,它似乎会破坏数据并将其拆分为每个字符,而不仅仅是逗号和换行符.我错过了一步吗?这是我使用公共表制作的示例:

#!/usr/bin/python

import csv
import urllib2, urllib

request_url = 'https://www.google.com/fusiontables/api/query' 
query = 'SELECT * FROM 1140242 LIMIT 10'

url = "%s?%s" % (request_url, urllib.urlencode({'sql': query}))
serv_req = urllib2.Request(url=url)
serv_resp = urllib2.urlopen(serv_req)

reader = csv.reader(serv_resp.read())

for row in reader:
    print row #prints out each character of each cell and the column headings

最终我会使用 csv.DictReader 类,但基础 reader 也显示了问题

【问题讨论】:

    标签: python csv google-fusion-tables


    【解决方案1】:

    csv.reader() 接受一个类似文件的对象。

    改变

    reader = csv.reader(serv_resp.read()) 
    

    reader = csv.reader(serv_resp)
    

    或者,您可以这样做:

    reader = csv.DictReader(serv_resp)
    

    【讨论】:

    • 非常感谢,我知道我误解了应该如何使用某些东西
    【解决方案2】:

    导致问题的不是 CSV 模块。看看serv_resp.read() 的输出。尝试改用serv_resp.readlines()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-06
      • 2012-11-23
      • 2018-05-28
      • 1970-01-01
      • 2013-11-19
      • 2013-02-24
      • 2019-06-30
      • 1970-01-01
      相关资源
      最近更新 更多