Commit 852503a4 by gijs

Reading files line per line

parent 720d95e2
......@@ -20,43 +20,35 @@ from collections import OrderedDict
import os
# open file
source=open("data/glove.42B.300d.txt", "r")
#source=open("data/glovesample.txt", "r")
path = "data/glove.42B.300d.txt"
# path="data/glovesample.txt"
# declare regular expression, any letter
pattern = re.compile("[a-zA-Z]*")
zxy = {}
abc = {}
# read line per line
for line in source:
line = source.readline()
# find the word
cuts = re.findall(pattern, line)
# if there is a word
if cuts[0]:
# isolate word
word = cuts[0]
# calculate lenght of the word
cut = len(word)
# split line in a list of word & numbers
newline = line[cut:]
# add to unsorted dictionary
zxy[word] = newline
source.close()
abc = OrderedDict(sorted(zxy.items()))
for key, value in abc.items():
#print word in center of screen, in green
print("\t\t\t\t\t\t\t\t\t", green(key), "\n")
# print numbers
print(value, "\n")
# allow 1 second pause for legibility
time.sleep(1)
os.system('cls' if os.name == 'nt' else 'clear')
print("\n")
with open(path, 'r', 1) as source:
for line in source:
# find the word
cuts = re.findall(pattern, line)
# if there is a word
if cuts[0]:
# isolate word
key = cuts[0]
# calculate lenght of the word
cut = len(key)
# split line in a list of word & numbers
value = line[cut:]
print("\t\t\t\t\t\t\t\t\t", green(key), "\n")
# print numbers
print(value, "\n")
# allow 1 second pause for legibility
time.sleep(1)
os.system('cls' if os.name == 'nt' else 'clear')
print("\n")
source.close()
# 66 woorden per seconde als we in 8u zouden tonen
# alfabetisch rangeschikken in tweede script
\ No newline at end of file
# alfabetisch rangeschikken in tweede script
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment