Created
January 12, 2010 03:10
-
-
Save fourdollars/274850 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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