-2

I have a file with computer logs. When I open it in notepad++, the first few lines look like this -

#####################
[2015/07/03, 13:26:40.368,  INFO, 00004588] Legacy mode. Epoch number not found. Allowing message.
[2015/07/03, 13:26:40.368,  INFO, 00004588] >>>AgentGetState() called.
[2015/07/03, 13:26:40.368,  INFO, 00004588] AgentGetState Returning 00000000.<<<
[2015/07/03, 13:26:54.660,  INFO, 00010404] Subject name = am4prdapp09.fc.core.windows.net
[2015/07/03, 13:26:54.660,  INFO, 00010404] Legacy mode. Epoch number not found. Allowing message.
######################

Now, I read them in python like so -

f = open("log_file.log")
a = []
for i in f:
    a.append(i)

When I now look at the content:

a[0]

This is what I see -

'\xff\xfe[\x002\x000\x001\x005\x00/\x000\x007\x00/\x000\x003\x00,\x00 \x001\x003\x00:\x002\x006\x00:\x004\x000\x00.\x003\x006\x008\x00,\x00 \x00 \x00I\x00N\x00F\x00O\x00,\x00 \x000\x000\x000\x000\x004\x005\x008\x008\x00]\x00 \x00
\x00j\x00e\x00c\x00t\x00 \x00n\x00a\x00m\x00e\x00 \x00=\x00 \x00a\x00m\x004\x00p\x00r\x00d\x00a\x00p\x00p\x000\x009\x00.\x00f\x00c\x00.\x00c\x00o\x00r\x00e\x00.\x00w\x00i\x00n\x00d\x00o\x00w\x00s\x00.\x00n\x00e\x00t\x00\r\x00\n'

What am I missing?

6
  • 5
    What's the encoding of the file? Commented Mar 26, 2016 at 22:09
  • and your python version? Commented Mar 26, 2016 at 22:10
  • Works fine for me... a[0] = ##################### Commented Mar 26, 2016 at 22:13
  • 2
    On an unrelated side note, a = list(f) would do the trick without your 3-line for loop there. Commented Mar 26, 2016 at 22:13
  • @MaxNoe - Unicode. I guess I need to convert it to ASCII. I'll try and do that. Thanks. Commented Mar 26, 2016 at 22:16

1 Answer 1

2

Try:

f = open("log_file.log", 'r')
data = f.readlines()
for i in data:
    print i
>>> line
>>> ...
>>> ...
Sign up to request clarification or add additional context in comments.

2 Comments

You should use with open ...
Python's file objects are iterable to begin with. You should do for i in f (or more descriptively, for line in f). The only reason to call readlines() first is if you just want to eat the cost of front-loading the whole thing into memory.

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.