
23.06.2009, 22:45
|
|
Постоянный
Регистрация: 12.06.2008
Сообщений: 654
С нами:
9427413
Репутация:
973
|
|
Сообщение от [n]-c0der
парсить регуляркой, вообще очень просто.
Код:
import re
cortej = {}
with open('file1.txt','a+') as f1:
for word in f1:
txt1 = str(re.findall(r'(\d.*) ',word))[2:-2]
txt2 = str(re.findall(r' (.*)',word))[2:-2]
cortej[txt1]=txt2
print cortej
'''
Example:
file1:
127.0.0.1 34
128.0.0.2 43
134.546.123.1 21
result:
{'134.546.123.1': '21', '128.0.0.2': '43', '127.0.0.1': '34'}
'''
Ну вот и нахера там регулярка ?
Код:
some_dictionary = {}
with open("source.txt") as inpt:
for line in inpt:
line = line.translate(None, "\r\n").split(" ")
some_dictionary[line[0]] = line[1]
print some_dictionary
|
|
|