我有一个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