Python adding a space to the beginning of an input file?

Viewed 116

I am relatively new to python and am working on a simple program that reads some information from an input file into a list. I am using the most recent version (3.7.4). I am also getting different output in visual studio vs the python IDLE.

Here is the entire context of my input file:

min:1,2,3,4,5,6
max:1,2,3,4,5,6
avg:1,2,3,4,5,6

my code:

inFile  = open('input.txt', 'r', encoding = 'utf-8')
outFile = open('output.txt', 'w')

for line in inFile :

    line = line.strip('\n')
    print(line[0:3] + " " + line[3:6])

    operation = line[0:3]
    print(line)

    # Create list of integers
    num_list = line[4:]
    print(num_list)
    print("")

outFile.close()
inFile.close()

Output in Python IDLE 3.7.4:

mi n:1
min:1,2,3,4,5,6
:1,2,3,4,5,6

max :1,
max:1,2,3,4,5,6
1,2,3,4,5,6

avg :1,
avg:1,2,3,4,5,6
1,2,3,4,5,6

Output in VS, python 3.7.0

 min:1,2,3,4,5,6
:1,2,3,4,5,6

max:1,2,3,4,5,6
1,2,3,4,5,6

avg :1,
avg:1,2,3,4,5,6
1,2,3,4,5,6

Any help is greatly appreciated!

Edit: The output I am looking for at the moment is:

min :1,
min:1,2,3,4,5,6
1,2,3,4,5,6

max :1,
max:1,2,3,4,5,6
1,2,3,4,5,6

avg :1,
avg:1,2,3,4,5,6
1,2,3,4,5,6

The reason I have an output file is that I will be adding further functionality to the program once I get this part working properly.

1 Answers

With the release of Python 3.7.4 on July 8, 2019, encoding of text files defaulted to UTF-8 which is why you see the difference between the IDLE and VS behavior. Specifying the encoding as above is one solution. Better to upgrade Python installation. Currently at 3.9.5

Related