Reputation: 23893
I'm not sure why this happen, there are zero length in my json file.
0
I t supposedly to be like this,
1000
I'm afraid the comma
thing after each json object cause this issue. (My current json format)
{ A:"A"},{ B:"B"),...
The correct way is like this
{ A:"A"} { B:"B"),...
So how I can calculate all the length without removing the comma
?
My code
import json
githubusers_data_path = 'githubusers.json'
githubusers_data = []
githubusers_file = open(githubusers_data_path, "r")
for line in githubusers_file:
try:
data = json.loads(line)
githubusers_data.append(data)
except:
continue
print len(githubusers_data)
Sample
{
"login": "datomnurdin"
}, {
"login": "ejamesc"
},...
Upvotes: 0
Views: 1617
Reputation: 1304
there is an exception in your code:
import json
githubusers_data_path = 'githubusers.json'
githubusers_data = []
githubusers_file = open(githubusers_data_path, "r")
for line in githubusers_file:
try:
data = json.load(githubusers_file) # exception from here
githubusers_data.append(data)
except Exception, e:
print e
print len(githubusers_data) # so githubusers_data is always []
Mixing iteration and read methods would lose data
Upvotes: 0
Reputation: 6223
I think you're getting an exception that you're suppressing with try-except, because of the commas.
One solution would be to convert your file to a string first, stick a '[' and ']' around the string to convert it into a valid json
format, then use json.loads
to convert the string.
import json
githubusers_data_path = 'githubusers.json'
githubusers_file = open(githubusers_data_path, "r")
githubusers_string = ''.join(line for line in githubusers_file)
githubusers_string = '[{}]'.format(githubusers_string)
githubusers_data = json.loads(githubusers_string)
print len(githubusers_data)
githubusers_file.close()
Upvotes: 2