我有一個JSON輸出,我想在Excel中獲得。熊貓匹配行列值
我所試圖做的是配合重量列標題。 我可以得到這個輸出使用一些循環。
我想要得到的是所有的權重作爲第一列標題,如果它有值粘貼在其他NaN。
page = requests.get(mainurl)
data = json.loads(page.text)
for i in data['categories']:
for j in i['items']:
if a == 1: # so changes and appends keys per category (highlighted)
a=2 # so not true in this loop
s=tuple(j['prices'].keys())
ws.append(s)
PVAL=list(j['prices'].values())
ws.append(PVAL)# append the value
a=1 # makes true next category
p= []
for i in price: # I know this is absolute madness but dicts were getting sorted
i = str(i).replace("'",'').replace('{','').replace('}','')# get price values
p.append(i)
###apppend in excel
注:如你可以通過上面的代碼告訴,我是一個初學者。和上面的代碼可能已經相當有2-3行大熊貓:( 現在我與熊貓擺弄做到這一點,因爲我認爲這將是更快,更好
主要編輯:
所以我沒有,所以我做了這麼多的時間。
for i in data['categories']:
for j in i['items']:
PVAL=j['prices']
try:
ounce = PVAL['ounce']
except:
ounce = 'NaN'
try:
gram = PVAL['gram']
except:gram = 'NaN'
try:
twograms = PVAL['two_grams']
except:twograms='NaN'
try:
quarter=PVAL['quarter']
except:quarter='NaN'
try:
eighth=PVAL['eighth']
except:eighth='NaN'
try:
halfO=PVAL['half_ounce']
except:halfO='NaN'
try:
unit = PVAL['unit']
except:unit='NaN'
try:
halfgram = PVAL['half_gram']
except:halfgram='NaN'
name= j['name']
cat = j['category_name']
listname = j['listing_name']
namel.append(name)
catl.append(cat)
listnamel.append(listname)
halfOl.append(halfO)
halfgraml.append(halfgram)
unitl.append(unit)
eighthl.append(eighth)
twogramsl.append(twograms)
quarterl.append(quarter)
ouncel.append(ounce)
graml.append(gram)
然後將這些名單在Excel附加 我知道這不是Pythonic,但我仍然試圖找出一種很好的方式來做到這一點在熊貓。
如果你能提供你的JSON輸入,我們可以複製和粘貼的樣本,那麼你應該得到的答案更快。 – Unatiel
謝謝,我已經完成了。 –
什麼是您使用的.json URL? – kbball