EX 20 output showing weird characters

Trying to run EX 20, but the output gets all wonky when I do. I have also gone back and looked at my code and rewritten it multiple times, but it keeps spitting out the same thing. Google searches for weird output errors have said that it might be an Encoding issue, but in Atom I am using the recommended UTF-8 encoding.

Any help is greatly appreciated!

Here is my code:

from sys import argv

script, input_file = argv

def print_all(f):
print(f.read())

def rewind(f):
f.seek(0)

def print_a_line(line_count, f):
print(line_count, f.readline())

current_file = open(input_file, encoding=‘utf-16-sig’)

print(“First let’s print the while file:\n”)

print_all(current_file)

print(“Now let’s rewind, kind of like a tape.”)

rewind(current_file)

print(“Let’s print three lines:”)

current_line = 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

Here is my output in powershell:

First let’s print the while file:

ÿþ1

2

3

4
Now let’s rewind, kind of like a tape.
Let’s print three lines:
1 ÿþ1

2

3 2

my test.txt file is just

1
2
3
4

Thanks in advance!

Why are you using the utf-16-sig encoding when you open the file? What if you use utf-8 instead?

1 Like

I’m not entirely sure all the ins and outs of what that means, but I was using wordpad to make my text files, and so I tried making them in Atom (which I know uses the utf-8 coding) and somehow that fixed the problem. Thank you so much! I’d been beating my head against a wall for hours trying to figure out what was wrong.

1 Like