擅长:python、mysql、java
<p>这个答案只适用于JSON文件,在这种情况下,这是一种健壮的工作方式:</p>
<pre><code>import json
with open('yourJsonFile', 'r') as jf:
jsonFile = json.load(jf)
print('Length of JSON object before cleaning: ', len(jsonFile.keys()))
testJson = {}
keyList = jsonFile.keys()
for key in keyList:
if not key.startswith('SOMETEXT'):
print(key)
testJson[key] = jsonFile[key]
print('Length of JSON object after cleaning: ', len(testJson.keys()))
with open('cleanedJson', 'w') as jf:
json.dump(testJson, jf)
</code></pre>