将“.db”字典类型文件读入pandas DataFram

2024-10-02 04:27:34 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将包含以下数据的文件导入熊猫数据框?另存为“数据数据库“,一种我不熟悉的格式。在

{"hostname":"136.243.73.66","ip":"136.243.73.66","port":16600,"TCPPort":15600,"UDPPort":14600,"seen":1,"connected":0,"tried":0,"weight":1,"dateTried":null,"dateLastConnected":null,"dateCreated":{"$$date":1514997141045},"isTrusted":true,"key":"5d301e5f46bb6e0db9d379c19c451cc6905c09885e5529c7a3c5d2750674db5ab8c3e714edd4fd53fee86499db4f93f8","remoteKey":null,"lastConnections":[],"_id":"FTpfNM4c4OuXAS6d"}
    {"hostname":"45.77.187.45","ip":"45.77.187.45","port":16600,"TCPPort":15600,"UDPPort":14600,"seen":1,"connected":0,"tried":0,"weight":1,"dateTried":null,"dateLastConnected":null,"dateCreated":{"$$date":1514997141046},"isTrusted":true,"key":"c752b75fbc74eaba10745937d50abd2decf71c509aff49db6662a180ba76fa3f74e5118ad905adb3b6873c250270f85f","remoteKey":null,"lastConnections":[],"_id":"f6Gn2xXyoeMrSvi8"}
    {"hostname":"mainnet.deviota.com","ip":null,"port":16600,"TCPPort":15600,"UDPPort":14600,"seen":1,"connected":0,"tried":0,"weight":1,"dateTried":null,"dateLastConnected":null,"dateCreated":{"$$date":1514997141048},"isTrusted":true,"key":"a923372977f65fe08f472916f671a1749963cea36701682761307af8537c52d4e2414f4e5b471898ef84a0957b5deec3","remoteKey":null,"lastConnections":[],"_id":"oVKsMubQ5rtAhfpq"}
    {"hostname":"mainnet2.deviota.com","ip":null,"port":16600,"TCPPort":15600,"UDPPort":14600,"seen":1,"connected":0,"tried":0,"weight":1,"dateTried":null,"dateLastConnected":null,"dateCreated":{"$$date":1514997141049},"isTrusted":true,"key":"9aae219149f088c9295de31125fb1f39060dd4fe1540c048f2bd097375298703c43d49dc48bb609f708f8b6e2578f7f2","remoteKey":null,"lastConnections":[],"_id":"rkQpS6BimYvfDIZU"}

这是我能得到的最接近的,但目前标签仍然在表中。在

^{pr2}$

enter image description here


Tags: ipdateportnullhostnameweightseenconnected
2条回答

.db不是一种特定的文件类型,尽管它通常用于sqlite文件。然而,这似乎只是一系列JSON文档,每行一个。在

with open(file_path) as f:
    return [json.loads(x) for x in f]

您可以使用^{},因为它是带有参数lines=Truejson文件:

df = pd.read_json('sample.db', lines=True)
print (df)

   TCPPort  UDPPort               _id  connected                dateCreated  \
0    15600    14600  FTpfNM4c4OuXAS6d          0  {'$$date': 1514997141045}   
1    15600    14600  f6Gn2xXyoeMrSvi8          0  {'$$date': 1514997141046}   
2    15600    14600  oVKsMubQ5rtAhfpq          0  {'$$date': 1514997141048}   
3    15600    14600  rkQpS6BimYvfDIZU          0  {'$$date': 1514997141049}   

   dateLastConnected  dateTried              hostname             ip  \
0                NaN        NaN         136.243.73.66  136.243.73.66   
1                NaN        NaN          45.77.187.45   45.77.187.45   
2                NaN        NaN   mainnet.deviota.com           None   
3                NaN        NaN  mainnet2.deviota.com           None   

   isTrusted                                                key  \
0       True  5d301e5f46bb6e0db9d379c19c451cc6905c09885e5529...   
1       True  c752b75fbc74eaba10745937d50abd2decf71c509aff49...   
2       True  a923372977f65fe08f472916f671a1749963cea3670168...   
3       True  9aae219149f088c9295de31125fb1f39060dd4fe1540c0...   

  lastConnections   port  remoteKey  seen  tried  weight  
0              []  16600        NaN     1      0       1  
1              []  16600        NaN     1      0       1  
2              []  16600        NaN     1      0       1  
3              []  16600        NaN     1      0       1  

如果要分析dateCreated列中的字典值,请添加apply

^{pr2}$

相关问题 更多 >

    热门问题