這篇文章將為大家詳細講解有關(guān)json字符串與python字符串的區(qū)別是什么,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

API的應用通常會處理json數(shù)據(jù),剛好今天看到了json字符串和python字符串的區(qū)別,放一段代碼,區(qū)別一下子就看出來,的確json 庫為處理Json 數(shù)據(jù)提供了不少的便利。
import json
jsonString = '{"arrayOfNums":[{"number":0},{"number":1},{"number":2}],"arrayOfFruits":[{"fruit":"apple"},{"fruit":"banana"},{"fruit":"pear"}]}'
jsonObj = json.loads(jsonString)
print(jsonObj.get("arrayOfNums"))
print(jsonObj.get("arrayOfNums")[0].get('number'))
#json 是一個字符串形式的。 沒有g(shù)et方法
#python 字符串有g(shù)et方法 便于處理 json里面的數(shù)據(jù)下面是一段通過ip地址查詢地理位置信息的代碼,也貼上去,接口是免費的
import json
from urllib.request import urlopen
def getCountry(ipAddress):
response = urlopen("http://freegeoip.net/json/"+ipAddress).read().decode('utf-8')
responseJson = json.loads(response)
print(responseJson)
return responseJson.get("country_code")
print(getCountry("50.78.253.58"))(代碼來自python網(wǎng)絡(luò)數(shù)據(jù)采集)
剛好看到,在貼個庫的用法上去,urllib.request.urltrieve 可以根據(jù)鏈接把文件下載下來,上代碼好理解一些
from urllib.request import urlretrieve
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen('http://www.pythonscraping.com')
bs4 = BeautifulSoup(html,'xml')
imageLocation = bs4.find("a",{"id":"logo"}).find("img")['src']
urlretrieve(imageLocation,"logo.jpg") #urlretrieve 根據(jù)下載鏈接 可以把文件下載下來
#把logo下載在當前目錄,名字叫l(wèi)ogo.jpg關(guān)于json字符串與python字符串的區(qū)別是什么就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
當前題目:json字符串與python字符串的區(qū)別是什么-創(chuàng)新互聯(lián)
轉(zhuǎn)載來于:http://chinadenli.net/article32/dghjpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站制作、品牌網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、網(wǎng)站設(shè)計公司、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容