非常陌生的Python并没有在SO上找到具体的答案,但如果这看起来非常幼稚或已经在其他地方提前道歉。
我正在尝试打印' IncorporationDate'来自多个公共数据集的JSON数据。我将网址保存为csv文件,下面的代码段。我只是从一个url打印所有JSON数据,我不确定如何在所有csv url上运行它,并向csv写入IncorporationDate值。
非常欢迎任何基本指导或编辑!
try:
# For Python 3.0 and later
from urllib.request import urlopen
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import urlopen
import json
def get_jsonparsed_data(url):
response = urlopen(url)
data = response.read().decode("utf-8")
return json.loads(data)
url = ("http://data.companieshouse.gov.uk/doc/company/01046514.json")
print(get_jsonparsed_data(url))
import csv
with open('test.csv') as f:
lis=[line.split() for line in f]
for i,x in enumerate(lis):
print ()
import StringIO
s = StringIO.StringIO()
with open('example.csv', 'w') as f:
for line in s:
f.write(line)
csv片段:
http://business.data.gov.uk/id/company/01046514.json
http://business.data.gov.uk/id/company/01751318.json
http://business.data.gov.uk/id/company/03164710.json
http://business.data.gov.uk/id/company/04403406.json
http://business.data.gov.uk/id/company/04405987.json
答案 0 :(得分:1)
第一步,您必须阅读CSV中的所有网址
import csv
csvReader = csv.reader('text.csv')
# next(csvReader) uncomment if you have a header in the .CSV file
all_urls = [row for row in csvReader if row]
第二步,从网址
获取数据from urllib.request import urlopen
def get_jsonparsed_data(url):
response = urlopen(url)
data = response.read().decode("utf-8")
return json.loads(data)
url_data = get_jsonparsed_data("give_your_url_here")
第三步:
以下代码:
for each_url in all_urls:
url_data = get_jsonparsed_data(each_url)
with open('IncorporationDates.csv', 'w' ) as abc:
abc.write(url_data['primaryTopic']['IncorporationDate'])
答案 1 :(得分:1)
欢迎来到Python世界。
hist(df$foo, breaks = 5)
,因为它已经死了简单的api。下面的代码段执行我认为您想要的内容:
list(c("1_lower"="<num>","1_upper"="<num2>","2_lower"="<num3>","2_upper"="<num4>"))
键创建一个新的CSV文件。```
requests
```