# python中文本编码

源代码文件作为文本文件就必然是以某种编码形式存储代码的，python默认会认为源代码文件是asci编码，所以如果在代码中使用了中文（utf），则执行时会提示如下错误

```
SyntaxError: Non-ASCII character '\xe5' in file ./check_kvm_qemu_cpu on line 143, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
```

解决的方法是在代码的前端第2行添加(第一行是`#!/usr/bin/python`)

```
# -*- coding: utf-8 -*-
```

## 参考

* [PEP 263 -- Defining Python Source Code Encodings](https://www.python.org/dev/peps/pep-0263/)
* [Python中文编码问题](http://www.cnblogs.com/ymy124/archive/2012/06/23/2559282.html)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://huataihuang.gitbook.io/cloud-atlas-draft/develop/python/startup/python_encoding.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
