“扫描单引号字符串时 EOL"?(字符串中的反斜杠) [英] "EOL while scanning single-quoted string"? (backslash in string)

查看:48
本文介绍了“扫描单引号字符串时 EOL"?(字符串中的反斜杠)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

导入操作系统xp1 = "\文档和设置\"xp2 = os.getenv("用户名")打印xp1+xp2

给我错误

 文件1.p​​y",第 2 行xp1 = "\文档和设置\"^语法错误:扫描单引号字符串时 EOL

你能帮我看看问题吗?

解决方案

反斜杠字符被解释为转义符.对 Windows 路径使用双反斜杠:

<预><代码>>>>xp1 = "\\文档和设置\\">>>xp1'\\文档和设置\\'>>>打印 xp1\文档和设置\>>>

import os
xp1 = "\Documents and Settings\"
xp2 = os.getenv("USERNAME")
print xp1+xp2

Gives me error

 File "1.py", line 2 
xp1 = "\Documents and Settings\"
                               ^
SyntaxError: EOL while scannning single-quoted string

Can you help me please, do you see the problem?

解决方案

The backslash character is interpreted as an escape. Use double backslashes for windows paths:

>>> xp1 = "\\Documents and Settings\\"
>>> xp1
'\\Documents and Settings\\'
>>> print xp1
\Documents and Settings\
>>> 

这篇关于“扫描单引号字符串时 EOL"?(字符串中的反斜杠)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆