从json到CSV提取数据



我正在使用opensea响应文件json,尝试获取有关nft 的信息

json文件https://paste.pythondiscord.com/yazaxipiwe完整json文件https://cdn.discordapp.com/attachments/303906514266226689/1028843885906116658/opensea.json

我在提取性状数据方面遇到了问题,性状具有日动态数据,如一些性状数据为零,一些性状数据具有4到8个性状数据

如何提取这样的特征

name    link    traits_type(eye)    traits_type(fur)    traits_type(mouth)
APE 0   https://lh3.google.com  3d eye  red open-mouth 
APE1    https://lh3.google.com  brown   robot   moster
APE2    https://lh3.google.com  white   green   big teeth 
APE3    https://lh3.google.com  monster eye brown   sharp teeth

CSV链接

代码

file = json.load(open('opensea.json'))
name.append(file['name'])
link.append(file['image_url'])

但是如何获取性状数据呢?

可能需要使用for-循环将其转换为具有{type:value}的字典

data = {}
for item in file['traits']: 
key = item['trait_type']
value = item['value'] 
data[key] = value
print(key, value)

稍后您可以创建行(使用.get(),当项目不存在时,您可以设置默认值(

row = [
file['name'], 
file['image_url'], 
data.get('Eye', ''), 
data.get('Fur', ''), 
data.get('Mouth', '')
]

并添加到列表

all_rows.append(row)

然后将所有转换为DataFrame

df = pd.DataFrame(all_rows, columns=['name', 'link', 'traits_type(eye)', 'traits_type(fur)', 'traits_type(mouth)']

import json
import pandas as pd
file = json.load(open('opensea.json'))
print(file.keys())
all_rows = []
for asset in file['assets']:
data = {}
for item in asset['traits']: 
key = item['trait_type']
value = item['value'] 
data[key] = value
#print(key, value)

row = [
asset['name'],
asset['image_url'],
data.get('Fur', ''),
data.get('Eyes', ''),
data.get('Mouth', '')
]
all_rows.append(row)

for item in all_rows:
print(item)
df = pd.DataFrame(all_rows, columns=['name', 'url', 'fur', 'eyes', 'mouth'])
print(df[['name', 'fur', 'eyes', 'mouth']])

结果:

name           fur         eyes                     mouth
0   None         Robot       X Eyes                Discomfort
1   None         Robot   Blue Beams                      Grin
2   None         Robot           3d           Bored Cigarette
3   None       Cheetah        Bored                Tongue Out
4   None  Golden Brown       Closed                 Phoneme L
5   None         Brown       X Eyes               Dumbfounded
6   None         Cream        Crazy                     Bored
7   None  Golden Brown        Angry                     Bored
8   None        Zombie        Robot                     Bored
9   None         Brown       Sleepy                Small Grin
10  None           Dmt     Eyepatch                     Bored
11  None    Dark Brown    Bloodshot  Bored Unshaven Cigarette
# ...

如果你只需要一个特征,你可以使用

[i['value'] for i in file['traits'] if i['trait_type'] == 'the trait type you need'][0]

用于循环for i in file['traits']如果需要i['trait_type'],则创建包含i['value']的列表。然后用[0]获取列表中的第一项

否则,考虑

for i in file['traits']:
if i['trait_type'] == 'the_trait':
list_for_the_trait.append(i['value'])
...

相关内容

  • 没有找到相关文章

最新更新