【发布时间】:2016-03-10 07:49:16
【问题描述】:
我正在编写代码以通过从网站上抓取 html 表来生成 csv 文件。该函数将查看表<tr> 的每一行并将数据列存储在字典中,如下所示
def write_data():
table_date = get_data() # call function to get data from html table into a dict
// write table_date to csv
def get_data():
data = {}
for row in tr:
data['name'] = 'John'
data['id'] = 12
return data
这是一个简化版本,但本质上我需要一种方法来获取每个表行的字典对象 data 并将其写入 csv,其中的键将是标题行。有什么有效的方法来做到这一点?
【问题讨论】:
标签: python csv python-3.x dictionary file-io