Reputation: 1
I have md5sum.txt
file and a python program. Then I want to parse some strings from that file. I don't undestand why my program raises an exception as there should be two elements in the list:
import hashlib
checksum_raw = open('F:/md5sum.txt', 'r').read()
cs_list_raw = checksum_raw.split("\n")
cs_list = []
for i in cs_list_raw:
cs_list.append({
'sum' : i.split(' ')[0],
'path' : i.split(' ')[1]
})
print (cs_list[0])
D:\newfolder\py_projects>py test.py
Traceback (most recent call last):
File "test.py", line 9, in <module>
'path' : i.split(' ')[1]
IndexError: list index out of range
But this one works well:
import hashlib
checksum_raw = open('F:/md5sum.txt', 'r').read()
cs_list_raw = checksum_raw.split("\n")
cs_list = []
for i in cs_list_raw:
cs_list.append({
'sum' : i.split(' ')[0],
'path' : i.split(' ')[-1]
})
print (cs_list[0])
D:\newfolder\py_projects>py test.py
{'sum': 'cde56251d6cae5214227d887dee3bab7', 'path': './pics/red-upperleft.png'}
Here are some strings from txt file:
cde56251d6cae5214227d887dee3bab7 ./pics/red-upperleft.png
0730e775a72519aaa450a3774fca5f55 ./pics/red-lowerleft.png
cd8aa5e7fa11b1362ef1869ac6b1aa56 ./pics/blue-lowerleft.png
92091902d3ca753bb858d4682b3fc26b ./pics/logo-50.jpg
461cbc7ff94fdea8008cab34b611abb8 ./pics/blue-upperright.png
9e18ae797773b2677b1b7b86e2aff28d ./pics/blue-lowerright.png
...
Upvotes: 0
Views: 45
Reputation: 43
Python indexing works in such a way that -1 maps to the last element in the list. In your case, it looks like there is only one element in the list and hence both the index 0 and -1 map to the same element. You should double-check if all the lines follow the format you specified.
Upvotes: 1