-1

I have a text file that has the following data:

<miscellaneous text for several lines> User ID Name USERA Test User 1 USERB Test User 2 .... <more miscellaneous text for several lines> 

I am trying to pull the user ID and Names out so I can output it into a .csv file. So I imagine the first step would be to load the file and put the contents of the text file into a string

Here is the code I have:

with open('FILE.txt', 'rb') as f: file = f.read() print(file) 

but this outputs the following: enter image description here

Why is it loading in the unicode NULL character? What can I do to fix this?

Also, is there a more efficient way to do this? My plan was to just do regex matches but that is not very efficient

2
  • the text file might be encoded in a different encoding. Play around with the encoding keyword in open. Commented Jun 1, 2016 at 17:38
  • That's regular ASCII zero bytes; they happen to be Unicode zeros, too (and Latin-1 zeros, etc) but that's hardly relevant here. Commented Jun 1, 2016 at 17:44

1 Answer 1

2

Looks like the file uses UTF-16. Use the correct codec and you should be fine.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.