logpuzzle doubt

82 views
Skip to first unread message

san jeev

unread,
Dec 22, 2017, 12:53:28 AM12/22/17
to Python GCU Forum
hi,
I wish to get all matching urls into dict using this code:

  f=open(filename)
  text=f.read()
  dict_url={}
  for line in text:
    m=re.search(r'"GET (\S+).jpg',line)
    if m:
      ip=re.search(r'(\d+.\d+.\d+.\d+) -\s+',line)
      dict_url[m.group()]=ip.group()
      print(m,ip)
    print(dict_url)

but I am not getting any error/answer. 
Can anyone plz help?

Alexandr Bushuev

unread,
Jan 4, 2018, 11:17:16 AM1/4/18
to Python GCU Forum
text=f.read()

To read a file’s contents, call f.read(size), which reads some quantity of data and returns it as a string. size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned; it’s your problem if the file is twice as large as your machine’s memory. Otherwise, at most size bytes are read and returned. If the end of the file has been reached, f.read() will return an empty string ("").

so you get a string in text var. Then, for against this string isn't productive.

If you wish read file line by line simply use this contruction
f = open(filename, 'rU')
rez
= {}
for line in f:






пятница, 22 декабря 2017 г., 8:53:28 UTC+3 пользователь san jeev написал:
Reply all
Reply to author
Forward
0 new messages