Python SyntaxError:扫描三重引号字符串文字时的EOF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31439715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:00:30  来源:igfitidea点击:

Python SyntaxError: EOF while scanning triple-quoted string literal

pythonsyntaxsyntax-error

提问by Richard Paul Astley

My script is:

我的脚本是:

# -*- coding: utf-8 -*-
RAW_ZIP = """PK    ??F4“àμ?$=   tcp_host.exeì]
|G??]è%\èa/íμMá?|%ElàBê…|bü?è”#?MIzù£??<¢\·±¨UQ??ZμjU′??h?′”ZahQ—??B???73{ùà?~yüéáììì?yó%?6“???I??%,…N–*§x?4xF*ó????d.?81l1?j'4?Z?í?R;?‰MêièL`
...long string...
"Y?-+\è\n$NK?—u-èp‰f(OY3ò ?h&?"? ?E\>ó]làY? *?|DZV=è?4???×?3òND???R?p?H5?Heòóêxt??‰Yí2ta?E?"&-')r¢W?ˉA?k"?hv%r3\?&·ù$?R1a6?????P¨63ííy§?ú???¢|?aby63>8£z?n`–DT?UaV“cO§E??z??—Où?a|??P‘[???9YGr?ü?K(E??íj?<£>M|ù^–?|P?8ˉòw‘é'*3??h?????μ[]T°3CxY-?aU?3ê"RzY,??—o?è#H??'?“
>?a?m1è?÷‰ )?§ìKvD
c"""

But when i run it i get the error:

但是当我运行它时,我收到错误:

SyntaxError: EOF while scanning triple-quoted string literal

Why?

为什么?

采纳答案by User

Reason: Python thinks that within your string the file ends.

原因:Python 认为文件在您的字符串中结束。

Guessing: For some reason character 26 is EOF in some cases.

猜测:出于某种原因,字符 26 在某些情况下是 EOF。

Motivation:
Python files are text files whereas zip files are binary. You should not mix them because

动机:
Python 文件是文本文件,而 zip 文件是二进制文件。你不应该混合它们,因为

  • the text files have an encoding whereas binary files do not.
  • in Windows, Mac and Linux line endings are different. Text files may be changed accordingly.
  • 文本文件有编码,而二进制文件没有。
  • 在 Windows、Mac 和 Linux 中,行尾是不同的。文本文件可能会相应更改。

In both cases zip binary stuff will break.

在这两种情况下,zip 二进制文件都会损坏。

Solution:
Encoding.

解决方案:
编码。

>>> import base64
>>> base64.b64encode(b"""raw string""") # here you get the encoded result
b'cmF3IHN0cmluZw==' 
>>> base64.b64decode(b'cmF3IHN0cmluZw==') # this is part of your Python file.
b'raw string'