for original_filename in list(d.keys()):
    m = re.search('^(.*)\\.html$', original_filename)
if not m:
    print('Ignoring the file:', original_filename)
continue
output_filename = m.group(1) + '.arff'
with open(output_filename, 'w') as fp:
    fp.write(
        '@RELATION wordcounts\n@ATTRIBUTE word string\n@ATTRIBUTE count numeric\n@DATA\n'
        )
for word_and_count in list(d[original_filename].items()):
    fp.write('%s,%d\n' % word_and_count)
Creating an ARFF file from python output