这是我的Json文件
{
"highest_table": {
"items": [{
"key": "Human 1",
"columns": {
"Na$me": "Tom",
"Description(ms/2)": "Table Number One on the Top",
"A&ge": "24",
"Ge_nder": "M"
}
},
{
"key": "Human 2",
"columns": {
"Na$me": "John",
"Description(ms/2)": "Table Number One on the Top",
"A&ge": "23",
"Ge_nder": "M"
}
}
]
}
}
目标是删除列名称中的所有特殊字符(或者,如果方便的话,删除.json文件中的所有特殊字符),并返回.json文件。 我最初的想法是将其转换为熊猫,删除列标题中的特殊字符,然后将其转换回.json文件。
这是我到目前为止尝试过的。他们两个都只打印一行。
import json
from pandas.io.json import json_normalize
data_file = r"C:\characters.json"
with open(data_file) as data_file:
data = json.load(data_file)
df = json_normalize(data)
-
data_file = r"C:\characters.json"
df = pd.read_json(data_file)
如何提取列,删除特殊字符并将其放回.json文件中?
答案 0 :(得分:2)
有点问题-您必须为NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(activity);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { mBuilder.setColor(activity.getResources().getColor(R.color.pricify_red));
mBuilder.setSmallIcon(R.drawable.transparent_img);
} else {
mBuilder.setColor(activity.getResources().getColor(R.color.pricify_red));
mBuilder.setSmallIcon(R.drawable.app_logo);
}
mBuilder.setContentTitle(title)
.setContentText(msg);
Intent i = new Intent();
i.setComponent(new ComponentName(currentActivity.this, DesiredActivity.class));
PendingIntent pIntent = PendingIntent.getActivity(activity, 0, i, 0);
mBuilder.setContentIntent(pIntent);
NotificationManager mNotificationManager =
(NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mNotificationId, mBuilder.build());
提供完整的实现,但这应该可以解决您的问题。
fixkey
答案 1 :(得分:1)
坦率地说,这是丑陋的,但是我还没有找到更通用的方法。这是特定于您的特定JSON的(问题确实需要在API中解决)。
import json
response = """{
"highest_table": {
"items": [{
"key": "Human 1",
"columns": {
"Na$me": "Tom",
"Description(ms/2)": "Table Number One on the Top",
"A&ge": "24",
"Ge_nder": "M"
}
},
{
"key": "Human 2",
"columns": {
"Na$me": "John",
"Description(ms/2)": "Table Number One on the Top",
"A&ge": "23",
"Ge_nder": "M"
}
}
]
}
}"""
def fix_json(resp):
output = {'highest_table': {'items': []}}
for item in resp['highest_table']['items']:
inner_dict = item['columns']
fixed_values = {'Name': inner_dict['Na$me'],
'Description(ms/2)': inner_dict['Description(ms/2)'],
'Age': inner_dict['A&ge'],
'Gender': inner_dict['Ge_nder']
}
new_inner = {'key': item['key'], 'columns': fixed_values}
output['highest_table']['items'].append(new_inner)
return output
response = json.loads(response)
fixed = fix_json(response)