0

I am trying to generate output files in such a way, first 7 entities into first json file, and next 7 entities into second json file, so on until the end.

data_path = 'C:/Users/User/Desktop/Tables/'
for i, data_item in enumerate(data):
    with open(data_path + str(i) + '.json', 'w') as outfile:
        outfile.write(simplejson.dumps({'entities': [data]}, indent=4, ignore_nan=True))

My script above, generates 1 entity for 1 Json file, but I really need 7 entities for each output Json file.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
satya veni
  • 17
  • 1
  • 8

1 Answers1

0

You're only getting 1 entity per JSON file because you're only iterating 1 entity at a time

for i, data_item in enumerate(data):

Also, based on the code in the question, I'm assuming you're iterating a files in a directory (although as written, you'll just be iterating characters in your string, but I digress).

To get the 7 entities per JSON you're looking for, you'll need to create that list per iteration, or iterate the list in chunks of 7 (I recommend the chunks method here).

data_path = 'C:/Users/User/Desktop/Tables/'
# Assume data is a list of filenames in a directory

# In this case, data_item will be a list of *up to* 7 entities
for i, data_item in enumerate(chunks(data, 7)):
    with open(f'{data_path}{i}.json', 'w') as outfile:
        outfile.write(simplejson.dumps({'entities': data_item}, indent=4, ignore_nan=True))
b_c
  • 1,202
  • 13
  • 24
  • Thanks for the reply. However I am getting below error.--------------------------------------------------------------------------- NameError Traceback (most recent call last) in 3 4 # In this case, data_item will be a list of *up to* 7 entities ----> 5 for i, data_item in enumerate(chunks(data, 7)): 6 with open(f'{data_path}{i}.json', 'w') as outfile: 7 outfile.write(simplejson.dumps({'entities': data_item}, indent=4, ignore_nan=True)) NameError: name 'chunks' is not defined – satya veni Sep 17 '19 at 13:36
  • You need to include the `chunks` method from the linked SO post in my answer. – b_c Sep 17 '19 at 14:25