Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中将字符串转换为字符串文字?_Python_String_Arcgis - Fatal编程技术网

如何在python中将字符串转换为字符串文字?

如何在python中将字符串转换为字符串文字?,python,string,arcgis,Python,String,Arcgis,我正在编写一些代码,使用Onclick事件获取一些文件路径。我需要确保这些文件路径是文本,以确保它们是正确的,以便我的其余代码可以运行。现在我想我得到的文件路径是unicode。基本上我需要这个: u"File\location\extra\slash" 为此: r"File\location\extra\slash" 我该怎么做?我还没有找到任何人真正能够成功地做到这一点,文档中也没有任何这样的例子。我无法更改为我提供文件路径Onclick事件的函数的工作方式 以下是相关代码: class

我正在编写一些代码,使用Onclick事件获取一些文件路径。我需要确保这些文件路径是文本,以确保它们是正确的,以便我的其余代码可以运行。现在我想我得到的文件路径是unicode。基本上我需要这个:

u"File\location\extra\slash"
为此:

r"File\location\extra\slash"
我该怎么做?我还没有找到任何人真正能够成功地做到这一点,文档中也没有任何这样的例子。我无法更改为我提供文件路径Onclick事件的函数的工作方式

以下是相关代码:

class SetLayer(object):
    """Implementation for leetScripts_addin.button2 (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        self.a = pythonaddins.GetSelectedCatalogWindowPath()
        print self.a
        #code split up path here
        self.b = os.path.split(str(self.a))
        self.c = self.b[0]
        self.d = os.path.split(self.c)
        self.e = (self.b[1])
        self.f = (self.d[1])
        self.g = (self.d[0])
你可以使用eval

MacBookPro:~ DavidLai$ python
Python 2.7.11 (default, Jan 22 2016, 08:29:18)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = u"File\location\extra\slash"
>>> y = eval("r\"" + x + "\"")
>>> y
'File\\location\\extra\\slash'
>>> type(y)
<type 'str'>
>>>
MacBookPro:~DavidLai$python
Python 2.7.11(默认,2016年1月22日,08:29:18)
[GCC 4.2.1达尔文兼容苹果LLVM 7.0.2(clang-700.1.81)]
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>x=u“文件\位置\额外\斜杠”
>>>y=eval(“r\”“+x+”\“”)
>>>y
'文件\\位置\\额外\\斜杠'
>>>类型(y)
>>>

从您的评论中,您有
a=u'File\\location\\extra\\slash'
,您想要提取
e='slash'
f='extra'
,和
g='File\location'
。这里不需要将字符串转换为字符串文字;您刚刚被各种级别的字符串转义弄糊涂了

您需要决定
e
f
g
应该是Unicode字符串还是ByTestRing。Unicode字符串可能是正确的选择,但我不能为您做出选择。无论您选择什么,您都需要确保始终知道您是在处理Unicode字符串还是ByTestRing。目前,
a
是一个Unicode字符串

如果您想要为
e
f
g
使用Unicode字符串,您可以这样做

self.e, temp = os.path.split(self.a)
self.g, self.f = os.path.split(temp)

如果需要ByTestRing,则需要使用适当的编码对self.a进行编码,然后执行上面的
os.path.split
调用。适当的编码方式取决于您的特定操作系统和应用程序
sys.getfilesystemencoding()
'utf-8'
都是可能的选择。

请参考:如果您说您有
u“File\location\extra\slash”
,听起来您对实际拥有的东西有误解。另外,除非您真的想打印前面有“r”和双引号的输出,否则您需要的实际上不是字符串文字。你能
打印repr(你的当前字符串)
并告诉我们它显示了什么吗?它不只是u“File\location\extra\slash”。decode('utf-8')?在重读之后,如果你只需要原始字符串将其传递到其他地方,这应该会起作用:
yourPath.encode('string-escape')
你的问题没有意义。。。请提供实际代码和实际输出。。。vs你希望输出是什么或者为什么你认为它不正确。。。。我们还不需要从悬崖上跳下去。。。(顺便说一句,如果路径是
“File\next\location”
,您的解决方案将无法工作)评估来自js的字符串(例如web客户端和可能不受信任的用户)是一个非常糟糕的主意。
eval('os.system('rm-rf/something/important'))
这只是一个随机事件,我在哪里转义字符串?@Steve:你发布的代码没有转义,但是原始字符串语法自动转义反斜杠,并且
u“File\location\extra\slash“
看起来您手动混淆了两种不同级别的转义。您第一次在注释中也使用了
repr
错误(
repr
进行了一些转义;它实际上是Python最接近的“将字符串转换为字符串文字”,尽管这不是您真正需要的。)我是如何错误地使用它的?有什么资源可以让我学习更多关于字符串如何实际工作的知识吗?我不知道这有什么不同。