4

I have a simple code over here that should output 2 columns of characters in a text file.

infile = open('anything.txt', 'r')
outfile = open('some.txt', 'w')
f = infile.readlines()
data=[]
a=['1','2','3']
b=['5','6','7']
for i in a:
  for j in b:
    outfile.write(i + "\t" + j + "\n")

What I got when I open the resultant text file with the standard Notepad are these strange characters! ऱਵऱਸ਼ऱ਷लਵलਸ਼ल਷ळਵळਸ਼ळ਷

However, when I opened the text file with Notepad++ or Wordpad, the result is two columns of numbers with a tab between them, as what we expected.

I am really lost here. What is going on ? Can't I open the text file with a standard Notepad?

Thanks for your help.

Levon
  • 138,105
  • 33
  • 200
  • 191
Poker Prof
  • 169
  • 5
  • 15
  • 2
    Note that you're not using `f` in the code snippet. Is this what the script looks like as you're running it, or have you made changes? (I don't have the problem either). – Justin Blank Jun 13 '12 at 13:48
  • LOL so strange right ? Anyway I tried open the file again in another comp with Windows 7 Notepad, the result is correct. So is it due to the old Notepad version in Windows XP? – Poker Prof Jun 13 '12 at 13:50
  • No, I'm on XP, so that's not it, at least not by itself. Does the problem persist on XP? – Justin Blank Jun 13 '12 at 13:50
  • Yah no matter what i changed, the weird characters always show when I open the textfile in XP notepad. Any other easy way to output such table as text file ? – Poker Prof Jun 13 '12 at 13:53
  • @MelvinAng Have you tried deleting the file? I think you're not writing to the file you think you are. – Marcin Jun 13 '12 at 16:01

3 Answers3

3

I believe it is a bug with Notepad.

Notepad is interpreting the data in the file as Unicode when it is ASCII. The first two characters are 1 and tab their ascii hex values are 31 and 09 If notepad mistakes the file for Unicode it will read the two values as one 3109 and display one character to match: http://www.unicodemap.org/details/0x0931/index.html (You can see that this matches the first character in your string of "strange characters".)

This is a well known bug with notepad and even has it's own humorously titled page on wikipedia: http://en.wikipedia.org/wiki/Bush_hid_the_facts

You can pick the character encoding in notepad to force the file open in the correct encoding by selecting it in the encoding drop-down (ANSI in this case). But it may be better to use another text editor if you want to see the correct values of data in text files.

Chris Kent
  • 862
  • 11
  • 18
2

I don't have this problem, what version of Python are you using, what OS?

You should explicitly close your files when you are done.

infile.close()
infile.close()

Better yet consider using with as it will close your files for you "automagically" when you are done or an exception is encountered:

with open('data.txt') as infile, open('some.txt', 'w') as outfile:

With earlier versions of Python (pre-2.7?) you may have to break this down into two:

with open('data.txt') as infile:  # default mode is "read" if not specified
  with open('some.txt', 'w') as outfile:

(Given that you mentioned you use Python v2.4 with won't work for you, it was introduced in v2.5 - still it's good to know about it)

I get this output:

1   5
1   6
1   7
2   5
2   6
2   7
3   5
3   6
3   7

Also, note you are not using these three lines in your program at all:

infile = open('anything.txt', 'r')
f = infile.readlines()
data=[]
Levon
  • 138,105
  • 33
  • 200
  • 191
  • So weird, Yah I got this output when I open the text file with Wordpad or Notepad++ . Is it because of the old version of Notepad or something ? I am on Windows XP and using Python 2.4.. hmmm – Poker Prof Jun 13 '12 at 13:49
  • @MelvinAng No idea. v 2.4 is pretty "dated" .. if you have a choice consider upgrading to at least 2.7. By the way, I used Notepad on XP too. Version 6.7 - just checked (no idea how current/old this is, rarely use notepad) – Levon Jun 13 '12 at 13:50
  • Sorry I have no choice. It is used in my company unfortunately. So it is either the Notepad version in Win XP or the old Python version. – Poker Prof Jun 13 '12 at 13:51
  • Anyway. this is the easiest method to output data in a table format into a text file right? Or do I have other options? – Poker Prof Jun 13 '12 at 13:51
  • Thx Levon. Guess there is nth I can do about that Notepad in the OS. – Poker Prof Jun 13 '12 at 13:58
  • 1
    @MelvinAng, there's absolutely nothing in the code snippet that you gave us that would produce the result you are seeing, no matter what version of Python or Notepad you are using. Is there anything writing to the output file that you left out of the question? – Mark Ransom Jun 13 '12 at 15:28
0

The different editors might assume a different character encoding. This would explain why some editors show the result correctly.

Karl Bartel
  • 3,244
  • 1
  • 29
  • 28