0

I have this .log file that I changed the extension name into .txt file but it still reads as log file

enter image description here

but after I copied it and paste it a new editor and saved it as .txt file.. this is what it showed:

enter image description here

Somebody told me that it is a non-ASCII characters that I should delete. Is there any way to delete it or any way to copy the contents of a log file then place it in a text file using python?

  • 1
    This looks like UTF-16. You should probably simply `iconv -f utf-16le trendx.log >trendx.txt` – tripleee Oct 05 '18 at 09:00
  • For the more general case maybe look at https://stackoverflow.com/questions/6609895/efficiently-replace-bad-characters (not the accepted answer). – tripleee Oct 05 '18 at 09:17

1 Answers1

2

In Python you can specify the input encoding.

with open('trendx.log', 'r', encoding='utf-16le') as reader, \
     open('trendx.txt', 'w') as writer:
   for line in reader:
        if "ROW" in line:
           writer.write(line)

I have obviously copied over some stuff from your earlier questions. Kudos for finally identifying the actual problem.

Notice in particular how we avoid reading the entire file into memory and instead processing a line at a time.

Alfe
  • 56,346
  • 20
  • 107
  • 159
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • but it only prints 3lines –  Oct 05 '18 at 13:08
  • Do you have more than three lines which contain `"ROW"`? Or maybe take out the `if` to print everything unconditionally. – tripleee Oct 06 '18 at 11:39
  • here is my log file please help me http://www.mediafire.com/file/idxhn1j4ev1wwl8/trendx.log.log/file this is my only problem :( –  Oct 06 '18 at 14:00
  • Not sure which "this" is your problem now, maybe ask a new question (perhaps on [unix.se] if it's not programming related?) – tripleee Oct 06 '18 at 19:06