我正在尝试创建一个脚本,该脚本基本上允许我从可以插入 SQL DB 的行中创建一个包含特定项目的列表。我在文本文件“addresses.txt”中有多行如下所示:
{"status":"OK","message":"OK","data":[{"type":"addressAccessType","addressAccessId":"0a3f508f-e7c8-32b8-e044-0003ba298018","municipalityCode":"0766","municipalityName":"Hedensted","streetCode":"0072","streetName":"Værnegården","streetBuildingIdentifier":"13","mailDeliverySublocationIdentifier":"","districtSubDivisionIdentifier":"","postCodeIdentifier":"8000","districtName":"Århus","presentationString":"Værnegården 13, 8000 Århus","addressSpecificCount":1,"validCoordinates":true,"geometryWkt":"POINT(553564 6179299)","x":553564,"y":6179299}]}
例如我想删除
"type":"addressAccessType","addressAccessId":"0a3f508f-e7c8-32b8-e044-0003ba298018"
最后得到一个列列表和一个可以写入 file_output.txt 的值列表,例如:
INSERT INTO ADDRESSES (%s) VALUES (%s)
这是我到目前为止所拥有的
# Writes %s into the file output_data.txt
address_line = """INSERT INTO ADDRESSES (%s) VALUES (%s)"""
# Reads every line from the file messy_data.txt
messy_string = file("addresses.txt").readlines()
cols = messy_string[0].split(",") #Defines each word in the first line separated by , as a column name
colstr = ','.join(cols) # formatted string that will plug in nicely
output_data = file("output_data.txt", 'w') # Creates the output file: output_data.txt
for r in messy_string[0:]: # loop through everything after first line
#r = r.replace(':',',')
#temp_replace = r.translate(None,'"{}[]()')
#address_list = temp_replace.split(",")
#address_list = [x.encode('utf-8') for x in address_list]
vals = r.split(",") # split at ,
valstr = ','.join(vals) # join with commas for sql
output_data.write(address_line % (colstr, valstr)) # write to file
output_data.close()
如果包括我的一些评论尝试,也许它会有所帮助。我还注意到,当我使用时,我#address_list = temp_replace.split(",")
所有的 utf-8 字符都被搞砸了,我不知道为什么或如何纠正这个问题。
更新 查看此示例如何将 JSON 转换为 CSV? 我想出了这段代码来解决我的问题:
# Reads every line from the file coordinates.txt
messy_string = file("coordinates.txt").readlines()
# Reads with the json module
x = json.loads(messy_string
x = json.loads(x)
f = csv.writer(open('test.csv', 'wb+'))
for x in x:
f.writerow([x['status'],
x['message'],
x['data']['type'],
x['data']['addressAccessId'],
x['data']['municipalityCode'],
x['data']['municipalityName'],
x['data']['streetCode'],
x['data']['streetName'],
x['data']['streetBuildingIdentifier'],
x['data']['mailDeliverySublocationIdentifier'],
x['data']['districtSubDivisionIdentifier'],
x['data']['postCodeIdentifier'],
x['data']['districtName'],
x['data']['presentationString'],
x['data']['addressSpecificCount'],
x['data']['validCoordinates'],
x['data']['geometryWkt'],
x['data']['x'],
x['data']['y']])
但是,这并不能解决我的问题,现在我收到以下错误
Traceback (most recent call last):
File "test2.py", line 10, in <module>
x = json.loads(messy_string)
File "C:\Python27\lib\json\__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
任何人都可以帮忙吗?提前致谢。