將資料變為json

2021-08-05 19:00:09 字數 4487 閱讀 3093

如果二級標記「k」值包含存在問題的字元,則應忽略

如果二級標記「k」值以「addr:」開頭,則應新增到字典「address」中

如果二級標記「k」值不是以「addr:」開頭,但是包含「:」,你可以按照自己認為最合適的方式進行處理。例如,你可以將其拆分為二級字典,例如包含「addr:」,或者轉換「:」以建立有效的鍵。

如果有第二個用於區分街道型別/方向的「:」,則應該忽略該標記,例如

lower = re.compile(r'^([a-z]|_)*$')

lower_colon = re.compile(r'^([a-z]|_)*:([a-z]|_)*$')

problemchars = re.compile(r'[=\+/&<>;\'"\?%#$@\,\. \t\r\n]')

created = [ "version", "changeset", "timestamp", "user", "uid"]

def shape_element(element):

node = {}

pos =

node_refs =

created = {}

address = {}

if element.tag == "node" or element.tag == "way" :

temp = {}

temp = element.attrib

if element.tag == "node":

node['id'] = temp['id']

node['type'] = element.tag

try:

node['visible'] = temp['visible']      

except:pass

node['pos'] = pos

created[created[0]] = temp['version']

created[created[1]] = temp['changeset']

created[created[2]] = temp['timestamp']

created[created[3]] = temp['user']

created[created[4]] = temp['uid']

node['created'] = created

for item1 in element:

if re.search(problemchars,item1.get('k')) == none:

if re.search('addr:',item1.get('k')) == none:

if re.search('amenity',item1.get('k')) != none:

node['amenity'] = item1.get('v')

elif re.search('cuisine',item1.get('k')) != none:

node['cuisine'] = item1.get('v')

elif re.search('name',item1.get('k')) != none:

node['name'] = item1.get('v')

elif re.search('phone',item1.get('k')) != none:

node['phone'] = item1.get('v')

else:pass

else:

if re.search(r'housenumber$',item1.get('k')) != none:

address['housenumber'] = item1.get('v')

elif re.search(r'postcode$',item1.get('k')) != none:

address['postcode'] = item1.get('v')

elif re.search(r'street$',item1.get('k')) != none:

address['street'] = item1.get('v')

else:pass  

else:

print(re.search(problemchars,item1.get('k')))

continue

if len(address) != 0:

node['address'] = address

else:

pass

else:

node['id'] = temp['id']

node['type'] = element.tag

node['visible'] = temp['visible']      

created[created[0]] = temp['version']

created[created[1]] = temp['changeset']

created[created[2]] = temp['timestamp']

created[created[3]] = temp['user']

created[created[3]] = temp['uid']

node['created'] = created            

for item in element:

if item.tag == 'nd':

else:

if re.search('addr:',item.get('k')) == none:

pass

else:

if re.search(r'housenumber$',item.get('k')) != none:

address['housenumber'] = item.get('v')

elif re.search(r'postcode$',item.get('k')) != none:

address['postcode'] = item.get('v')

elif re.search(r'street$',item.get('k')) != none:

address['street'] = item.get('v')

print(address['street'])

else:pass  

node['node_refs'] = node_refs

if len(address) != 0:

node['address'] = address

else:

pass

#print(item.tag)

#print("el",temp)

#print('node',node)

# your code here

return node

else:

return none

def process_map(file_in, pretty = false):

# you do not need to change this file

file_out = ".json".format(file_in)

data =

with codecs.open(file_out, "w") as fo:

for _, element in et.iterparse(file_in):

el = shape_element(element)

if el:

if pretty:

fo.write(json.dumps(el, indent=2)+"\n")

else:

fo.write(json.dumps(el) + "\n")

return data

def test():

# note: if you are running this code on your computer, with a larger dataset, 

# call the process_map procedure with pretty=false. the pretty=true option adds 

# additional spaces to the output, ****** it significantly larger.

data = process_map('example.osm', true)

pprint.pprint(data[-1])

correct_first_elem =

}assert data[0] == correct_first_elem

assert data[-1]["address"] ==

assert data[-1]["node_refs"] == [ "2199822281", "2199822390",  "2199822392", "2199822369", 

"2199822370", "2199822284", "2199822281"]

if __name__ == "__main__":

test()

QT將窗體變為頂層窗體

在windows 上通過滑鼠雙擊某應用程式圖示,該應用程式往往會以頂層視窗的形式呈現在我們面前,但是對於乙個已經開啟的非頂層視窗,我們怎麼將其啟用為頂層視窗呢?要達到啟用,這個必須要滿足兩個條件 有通知啟用的人,這個人必須是另乙個應用程式 或同一應用程式的不同示例 他能傳送資訊給該窗體 該窗體能夠解...

python將json資料轉成字典

做文字分類的時候,給定的資料集是json格式的,用jieba分詞需要用字典,而且給定的資料集只需要其中的兩個字段。所以第一步就是將json資料轉成只包含所需兩個欄位的字典 import json f open data test.json r encoding utf 8 for line in f...

python將json資料存入MySQL中

一 準備工作 安裝mysql 安裝資料庫操作工具,我使用的是mysql front 已經爬取好了的json檔案,之前有寫過,這裡直接拿來用。二 import json import pymysql defprem db cursor db.cursor cursor.execute select v...