栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何摆脱Python Windows文件路径字符串中的双反斜杠?[重复]

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何摆脱Python Windows文件路径字符串中的双反斜杠?[重复]

双反斜杠没有错,python向用户 表示 它的方式。在每个双反斜杠中

\
,第一个会 转义 第二个,以表示实际的反斜杠。如果
a = r'rawstring'
b = 'raw s\tring'
(没有’r’和显式双斜杠),则它们都表示为
'raw s\tring'

>>> a = r'raw string'>>> b = 'raw s\tring'>>> a'raw s\tring'>>> b'raw s\tring'

为了澄清起见,在 打印 字符串时,您会看到它的用法,就像在路径中一样-仅带有一个反斜杠:

>>> print(a)raw string>>> print(b)raw string

在这种打印的字符串情况下,

t
并不表示 制表符 ,它是一个反斜杠,
后跟字母“ t”。

否则,没有’r’前缀且带有单个反斜杠的字符串将转义 其后 的字符,从而使它在==选项卡 评估’t’:

>>> t = 'not raw string'  # here 't' = tab>>> t'not raw string'>>> print(t)  # will print a tab (and no letter 't' in 'string')not raw s       ring

因此,在PDF路径+名称中:

>>> item = 'xyz'>>> PDF = r'C:UsersuserDesktopFile_%s.pdf' % item>>> PDF         # the representation of the string, also in error messages'C:\Users\user\Desktop\File_xyz.pdf'>>> print(PDF)  # "as used"C:UsersuserDesktopFile_xyz.pdf

有关 转义序列的
更多信息,请参见表。另请参阅

__str__
VS
__repr__



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/646978.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号