简体   繁体   English

Python脚本在Spyder中完美运行(在Windows上) - 在Linux上不起作用

[英]Python script runs perfectly in Spyder (on Windows) - Doesn't work on Linux

I wrote a script that reads a .cnf file, analyses some stuff and then outputs some results (via print() ). 我写了一个脚本,它读取.cnf文件,分析一些东西,然后输出一些结果(通过print() )。 For reading the .cnf file, I use the following line: 要读取.cnf文件,我使用以下行:

with open('config.cnf') as f:
file_content = f.read()

Now if I run this in the Spyder-Environment (Python 3.6), everything works fine. 现在如果我在Spyder-Environment(Python 3.6)中运行它,一切正常。 The scripts read config.cnf, does the operations and outputs the results. 脚本读取config.cnf,执行操作并输出结果。 If I run the exact same script on Linux (with the config.cnf located in the same Directory), the following error message is shown: 如果我在Linux上运行完全相同的脚本(config.cnf位于同一目录中),则会显示以下错误消息:

Traceback (most recent call last):
  File "Conf2Monit_V2.py", line 45, in <module>
    file_content = f.read()
  File "/usr/lib/python3.6/codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x96 in Position 29834: invalid start byte

I use the following command: 我使用以下命令:

python3 myScript.py 

I am new to Python AND to Linux, so please don't be fed up if this is some basic mistake. 我是Python和Linux的新手,所以如果这是一个基本错误,请不要厌倦。 Thank you. 谢谢。

I'm guessing the issue could be because of Historical reason behind different line ending at different platforms 我猜这个问题可能是因为在不同平台的不同行结束背后历史原因

If this is the case then please give a try at this 如果是这种情况,那么请给在尝试

Else you can try the iconv command of Linux. 否则你可以试试Linux的iconv命令。

Can you check the encoding of the characters in the file by running the below command in your Linux environment: 您可以通过在Linux环境中运行以下命令来检查文件中字符的编码:

file -i <filename>

Check if the output is something like charset=utf-8 检查输出是否类似于charset=utf-8

If not then, there is a way to translate the encoding to UTF-8 as shown here 如果没有,那么,还有如翻译的编码设置为UTF-8的方式在这里

It explains how to convert the encoding of a file ("input.txt") from the code set ISO88592 to UTF8 code set or ASCII and stores the result as "output.txt". 它解释了如何将文件的编码(“input.txt”)从代码集ISO88592转换为UTF8代码集或ASCII,并将结果存储为“output.txt”。

iconv -f ISO88592 -t UTF8 input.txt output.txt

So, the steps you might want to follow are, if the file under consideration is input.txt: 因此,您可能要遵循的步骤是,如果正在考虑的文件是input.txt:

  1. file -i input.txt file -i input.txt
    and let's say its output comes something like 让我们说它的输出就像是
    input.txt: text/plain; input.txt:text / plain; charset=iso-88592 字符集= ISO-88592
  2. iconv -f ISO88592 -t UTF8 input.txt output.txt iconv -f ISO88592 -t UTF8 input.txt output.txt

The solution was simple: 解决方案很简单:

I opened the file in the Windows Editor. 我在Windows编辑器中打开了该文件。 Then under "save as..." I was able to Change a Setting at the bottom from "ASCII" to "UTF-8". 然后在“另存为...”下,我能够将底部的设置从“ASCII”更改为“UTF-8”。 Then I transferred the file back to my Linux System et voila. 然后我将文件传回我的Linux系统等。 Worked. 工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM