Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python在输入文件的开头添加空格?_Python_Python 3.x_Visual Studio_Python 3.7_Python Idle - Fatal编程技术网

Python在输入文件的开头添加空格?

Python在输入文件的开头添加空格?,python,python-3.x,visual-studio,python-3.7,python-idle,Python,Python 3.x,Visual Studio,Python 3.7,Python Idle,我对python比较陌生,正在编写一个简单的程序,将输入文件中的一些信息读取到列表中。我使用的是最新版本(3.7.4)。我在VisualStudio和python IDLE中也得到了不同的输出 以下是我的输入文件的整个上下文: min:1,2,3,4,5,6 max:1,2,3,4,5,6 avg:1,2,3,4,5,6 我的代码: infle=open('input.txt','r',encoding='utf-8') outFile=open('output.txt','w') 对于填充中

我对python比较陌生,正在编写一个简单的程序,将输入文件中的一些信息读取到列表中。我使用的是最新版本(3.7.4)。我在VisualStudio和python IDLE中也得到了不同的输出

以下是我的输入文件的整个上下文:

min:1,2,3,4,5,6
max:1,2,3,4,5,6
avg:1,2,3,4,5,6
我的代码:

infle=open('input.txt','r',encoding='utf-8')
outFile=open('output.txt','w')
对于填充中的线:
line=line.strip('\n')
打印(行[0:3]+“”+行[3:6])
操作=行[0:3]
打印(行)
#创建整数列表
num_list=行[4:]
打印(数字列表)
打印(“”)
outFile.close()
infle.close()
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
在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
非常感谢您的帮助

编辑: 我目前正在寻找的结果是:

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

我有一个输出文件的原因是,一旦这部分工作正常,我将为程序添加更多功能。

随着2019年7月8日Python 3.7.4的发布,文本文件的编码默认为UTF-8,这就是为什么您会看到空闲和VS行为之间的差异。如上所述指定编码是一种解决方案。最好升级Python安装。目前为3.9.5

您的目标输出是什么?变量
min
max
avg
与python对象相关联的列表?或者只是打印文件的内容?如果不在文件中写入任何内容,为什么要打开文件进行输出?可能是输入文件开头的字节顺序标记。尝试
encoding='utf-8-sig'
读取文件。3字节移位,Michael可能是对的。而Python3.7.0没有考虑到这一点account@MichaelButscher这个编码对我有用!非常感谢你!最好不要依赖于更改默认值并始终指定编码。