Skip to content

Instantly share code, notes, and snippets.

@fourdollars
Created January 12, 2010 03:10
Show Gist options
  • Save fourdollars/274850 to your computer and use it in GitHub Desktop.
Save fourdollars/274850 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# generate json data from txt file
import re
def main():
words = []
file = open("words.txt")
while True:
line = file.readline()
if not line:
break
line = str.strip(line)
if line and line[0] != '#':
fields = re.split(' ', line)
words.append(fields[0])
file.close()
for i, word in enumerate(words):
j = i + 1
while j < len(words):
if word == words[j]:
del words[j]
j = j + 1
print "({"
print " 'word': ["
for i, word in enumerate(words):
if i == len(words) - 1:
print " '%s'" % (word)
else:
print " '%s'," % (word)
print " ],"
print " 'length': %d" % (len(words))
print "})"
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment