I want to save data in text files and create dictionaries from those files, which I’ll pass to a function later on.
Here’s my code:
def lesson_dictionary(filename):
print "Reading file ", filename
with open(filename) as f:
mylist = f.read().strip().split()
dictionary = OrderedDict(zip(mylist[::2], mylist[1::2])) #keep keys/values in same order as declared in mylist
print dictionary
return dictionary
With a sample file named sample.txt containing two columns of key/value pairs separated by a space, it works fine. For example,
a b
c d
e f
yields a list like so:
OrderedDict([('a', 'b'), ('c', 'd'), ('e', 'f')])
BUT if I change the code and the content of the .txt file, it breaks. For example, if sample2.txt included:
a:b
c:d
e:f
and my code is
def lesson_dictionary(filename):
print "Reading file ", filename
with open(filename) as f:
mylist = f.read().strip().split(':') #CHANGED: split string at colon!
dictionary = OrderedDict(zip(mylist[::2], mylist[1::2]))
print dictionary
return dictionary
I get the following output:
OrderedDict([('a', 'b \nc'), ('d\ne', 'f')])
What’s happening? Why did strip() work for the first .txt file but not for the second? Thanks in advance for any help.
The original
split()split on whitespace, and\nis considered whitespace. By changing tosplit(':')you’ve removed the split on the end of line, so the end of one line is merged with the start of the next with an extra newline character in the middle. I don’t think there’s an easy way to fix it except to read the file one line at a time.Edit: Some code to demonstrate.
Or more in the spirit of your original:
The second form has the disadvantage of not automatically stripping whitespace from around the words. Based on your example, you might need that.