【发布时间】:2019-08-29 23:49:03
【问题描述】:
我制作了一个从文档中读取 json 样式行的脚本。 几十行看起来像这样:
{'ip': 'text', 'type': 'ipv4', 'continent_code': 'EU', 'continent_name': 'Europe', 'country_code': 'CO', 'country_name': 'CO ', 'region_code': '82', 'region_name': 'Central', 'city': 'city', 'zip': '8900', 'latitude': text, 'longitude': text}
f = open("jsondmp.json","r")
for i in f:
#print(f.readlines())
json= f.readline()
sql = "Insert Into dbo.jsondmp values (%s)" % json
cur.execute(sql)
但是我向我抛出了一个错误..
syntax error at or near "{"
LINE 1: Insert Into dbo.jsondmp values ({'ip': '*TEXT*', 'type...
^
我尝试将字符串参数从 (%s)" % json 更改为 (?)", (json)
但它只是抛出一个
参数 1 必须是字符串或 unicode 对象:改为使用元组
我显然错过了一些基本的 Python 理论,但似乎无法更进一步。
我尝试进行测试运行,只是让脚本打印包含此处记录的 json 字符串的插入语句:docs for psycopg2
f = open("jsondmp.json","r")
for i in f:
#print(f.readlines())
json= f.readline()
print("Insert Into dbo.jsondmp values ('%s')" % json)
它给了我这个字符串
插入 dbo.jsondmp 值('{'ip': 'text', 'type': 'ipv4', 'continent_code': 'EU', 'continent_name': 'Europe', 'country_code': 'CO ', 'country_name': 'Co', 'region_code': '84', 'region_name': '首都地区', 'city': 'text', 'zip': '1050', 'latitude': 文本, '经度':文本} ')
看起来很对..
【问题讨论】: