我有Json文件,并将其转换为字典,然后从其中提取了一些数据,这些数据是类的名称。
这是我的代码
import pandas as pd
import json
df = pd.read_json("/content/Test1.json")
with open('/content/Test1.json') as handle:
dictdump = json.loads(handle.read())
for elem in dictdump :
print(elem['fullyQualifiedName'])
然后返回这些,它们是类的名称:
android.support.v4.app.Watson
android.support.v4.app.Watson.OnCreateOptionsMenuListener
android.support.v4.app.Watson.OnPrepareOptionsMenuListener
android.support.v4.app.Watson.OnOptionsItemSelectedListener
com.actionbarsherlock.ActionBarSherlock
com.actionbarsherlock.ActionBarSherlock.OnCreatePanelMenuListener
com.actionbarsherlock.ActionBarSherlock.OnCreateOptionsMenuListener
com.actionbarsherlock.ActionBarSherlock.OnMenuItemSelectedListener
com.actionbarsherlock.ActionBarSherlock.OnOptionsItemSelectedListener
com.actionbarsherlock.ActionBarSherlock.OnPreparePanelListener
我想将此数据插入CSV文件。任何解决方案中的每行类名称都请
答案 0 :(得分:0)
如果您的数据框是这样的,当您将json读取到df中时:
Show
undefined
End
那你可以做:
fullyQualifiedName
0 android.support.v4.app.Watson
1 android.support.v4.app.Watson.OnCreateOptionsM...
2 android.support.v4.app.Watson.OnPrepareOptions...
3 android.support.v4.app.Watson.OnOptionsItemSel...
4 com.actionbarsherlock.ActionBarSherlock
5 com.actionbarsherlock.ActionBarSherlock.OnCrea...
6 com.actionbarsherlock.ActionBarSherlock.OnCrea...
7 com.actionbarsherlock.ActionBarSherlock.OnMenu...
8 com.actionbarsherlock.ActionBarSherlock.OnOpti...
9 com.actionbarsherlock.ActionBarSherlock.OnPrep...
结果(csv文件的内容):
df.to_csv("test.csv",columns=["fullyQualifiedName"], index=False,header=False)
设置
android.support.v4.app.Watson
android.support.v4.app.Watson.OnCreateOptionsMenuListener
android.support.v4.app.Watson.OnPrepareOptionsMenuListener
android.support.v4.app.Watson.OnOptionsItemSelectedListener
com.actionbarsherlock.ActionBarSherlock
com.actionbarsherlock.ActionBarSherlock.OnCreatePanelMenuListener
com.actionbarsherlock.ActionBarSherlock.OnCreateOptionsMenuListener
com.actionbarsherlock.ActionBarSherlock.OnMenuItemSelectedListener
com.actionbarsherlock.ActionBarSherlock.OnOptionsItemSelectedListener
com.actionbarsherlock.ActionBarSherlock.OnPreparePanelListener
如果您还希望在csv中将列名用作标题。
答案 1 :(得分:0)
这是一种实现此目的的方法:
import pandas as pd
import json
classDF = pd.DataFrame(columns = ['NameOfClass', 'Type_of_smells'])
with open('/content/Test1.json') as handle:
dictdump = json.loads(handle.read())
for elem in dictdump :
class_dict = {'NameOfClass': elem['fullyQualifiedName'], 'Type_of_smells': elem['WhatEverYourJsonNameIs']}
class_name = pd.DataFrame.from_dict(class_dict, orient='index')
class_name = class_name.transpose()
classDF = pd.concat([classDF, class_name])
classDF.to_csv('/path/class_name.csv', mode='a', header=True)