0

I am trying to print each line from a text file into a console using IDLE with Python 2.

with open("Stats_test.txt") as f:
    for line in f:
        print line

When I run this file though the text from the file prints out with a space between each character. I am confused as to what I can do to fix it

Text in Text file

21/09/2014, 16:14 - Salim: How do you do it???
21/09/2014, 16:15 - Salim: <Media omitted>
21/09/2014, 16:15 - Olive: Do what!?!
21/09/2014, 16:16 - Olive: Jackie Chan!?!
21/09/2014, 16:16 - Olive: Do u know Jackie Chan can sing
21/09/2014, 16:16 - Salim: The math prob
21/09/2014, 16:16 - Salim: #funfact
21/09/2014, 16:17 - Salim: The math thing is unpossible
21/09/2014, 16:18 - Olive: Jo
21/09/2014, 16:18 - Olive: Jus like the word unpossible being in the dictionary
21/09/2014, 16:18 - Salim: Depends on where you get your dictionary from
30/09/2015, 22:27 - Salim: Like student tutors
30/09/2015, 22:27 - Salim: Duke of edinburgh
30/09/2015, 22:27 - Olive: Hahaha
30/09/2015, 22:27 - Olive: So who do u hang around with!?
30/09/2015, 22:28 - Salim: A korean
A Swedish 
An American 
An Austrian
30/09/2015, 22:28 - Olive: 
30/09/2015, 22:28 - Olive: I guess they have names
30/09/2015, 22:28 - Olive: But thts better than jus names
30/09/2015, 22:29 - Olive: I mostly have all indians
30/09/2015, 22:29 - Salim: It's diyafah
30/09/2015, 22:29 - Salim: And indians
30/09/2015, 22:29 - Olive: Lol yeah
30/09/2015, 22:29 - Salim: Oh and the swearing

Prints out like this though in the console. Picture

I think it is due to the Unicode encoding, can someone confirm? And if so how do I remove the redundant spaces without losing the Unicode because if I use ANSI it loses some text and data.

Salim
  • 439
  • 2
  • 8

2 Answers2

0

Your input file is unicode. That is apparent from the emojis. But your program doesn't know that and is interpreting the input as bytes.

This problem has an answer on SO at Character reading from file in Python but before your program will work you will have to decide what you want to do about the emojis.

The print statement automatically tries to encode a unicode string as ascii, and that will fail because the emojis aren't ascii.

BoarGules
  • 16,440
  • 2
  • 27
  • 44
-1

When you read lines like this they always end with \n, which is the newline character. When you print you add another \n to the line, which causes double spacing. You need to remove one of those, like this:

with open("Stats_test.txt") as f:
    for line in f:
        print line.rstrip()
Ofer Sadan
  • 11,391
  • 5
  • 38
  • 62