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

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

如何摆脱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__



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5649177.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存