0

I wrote these lines to import zip file with log files in it:

from google.colab import drive
drive.mount('/content/drive')

from zipfile import ZipFile
df_ht =  !unzip "/content/drive/MyDrive/HTWebLog_p1.zip"

To read the log file:

file = open('ex061101.log', 'r')
print(file)

output:

<_io.TextIOWrapper name='ex061101.log' mode='r' encoding='UTF-8'>

lines = file.read().splitlines()
lines[5:6]

Output:

['2006-11-01 00:00:08 W3SVC1 127.0.0.1 GET /Tulip/home/en-us/home_index.aspx - 80 - 70.80.84.76 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1) - 200 0 0']

Now I am trying to store the loop output, but I only get the last log file (judging by the length of it):

for i in range(212,229):
    lines = (open(str('ex061') + str(i) + str('.log'), 'r', encoding='iso-8859-1')).read().splitlines()
len(lines)

EDIT.

I found the solution:

import itertools

lines = [] 

for i in range(212,229): 
    lines = (open(str('ex061') + str(i) + str('.log'), 'r', encoding='iso-8859-1')).read().splitlines() 
output=list(itertools.chain(lines))
3
  • 1
    Yeah, you’re just overwriting the same variable again and again. You might want to make a list and append to it? Commented Apr 5, 2021 at 5:01
  • And, by the way, 'ex061' and '.log' are already strings, You don't have to wrap them in str(). You might even consider f"ex061{i}.log". Commented Apr 5, 2021 at 5:07
  • Thank you, I did it by: 'lines = []' before loop and after loop this line: output=list(itertools.chain(lines)) Commented Apr 5, 2021 at 5:08

1 Answer 1

1

In the for loop you write the variable lines but because it loops you over write it every time it loops which is why you are only getting the last values, a better way is to create a list and add to it every time you loop which will not over write it and store it nicely.

list = []

for i in range(212, 229):
   list.append(open(str('ex061') + str(i) + str('.log'), 'r', encoding='iso-8859-1')).read().splitlines())

I havent tested the code out but should work

Sign up to request clarification or add additional context in comments.

2 Comments

There is an error: AttributeError: 'NoneType' object has no attribute 'read'. Your previous deleted response didn't work as well because of that error: AttributeErrorError: sequence item 0: expected str instance, list found. But I got some ideas from that join function and eventually got this: lines = [] for i in range(212,229): lines = (open(str('ex061') + str(i) + str('.log'), 'r', encoding='iso-8859-1')).read().splitlines() output=list(itertools.chain(lines)) It works.
Good to know, I didnt test the code, i just wanted to show you the list and the append aspect of it all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.