Thanks all
- 加上 encoding = 'utf-8' 后,的确中文版的weather_info读取ok了(包括第一行),程序也运转正常。
- 但是英文版的weather_info2读取时第一行还是有问题,始终会有一个"\ufeff"出现在第一字符串Hangzhou之前。查询后得知原来问题如下: Python会自动加一些特殊字符串在代码的头部,但这些不会显示在你的文档中,等你执行代码时它就会报错,错误显示在第一行,如果用read()去看,就会在第一行看到"u\feff"
- 解决方案,按照如下介绍,我将英文版的文档内容拷贝到我的notepad++, 然后在保存时选择Encoding 下拉菜单中的"Encoding in ANSI", 保存为新的weather.txt文件,然后再跑程序,一切终于都正常了.....
"This problem arise basically when you save your python code in a UTF-8 or UTF-16 encoding because python add some special character at the beginning of the code automatically (which is not shown by the text editors) to identify the encoding format. But, when you try to execute the code it gives you the syntax error in line 1 i.e, start of code because python compiler understands ASCII encoding. when you view the code of file using read() function you can see at the begin of the returned code '\ufeff' is shown. *** The one simplest solution to this problem is just by changing the encoding back to ASCII encoding(for this you can copy your code to a notepad and save it Remember! choose the ASCII encoding...*** Hope this will help."