Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.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_Rawstring - Fatal编程技术网

如何从python中的字符串变量创建原始字符串?

如何从python中的字符串变量创建原始字符串?,python,string,rawstring,Python,String,Rawstring,通过以下方式从字符串创建原始字符串: test_file=open(r'c:\Python27\test.txt','r') 如何从字符串变量创建原始变量,例如 path = 'c:\Python27\test.txt' test_file=open(rpath,'r') 因为我有一个文件路径: file_path = "C:\Users\b_zz\Desktop\my_file" 当我这样做时: data_list = open(os.path.expandvars(file_path

通过以下方式从字符串创建原始字符串:

test_file=open(r'c:\Python27\test.txt','r')
如何从字符串变量创建原始变量,例如

path = 'c:\Python27\test.txt'

test_file=open(rpath,'r')
因为我有一个文件路径:

file_path = "C:\Users\b_zz\Desktop\my_file"
当我这样做时:

data_list = open(os.path.expandvars(file_path),"r").readlines()
我得到:

Traceback (most recent call last):
  File "<pyshell#32>", line 1, in <module>
    scheduled_data_list = open(os.path.expandvars(file_path),"r").readlines()
IOError: [Errno 22] invalid mode ('r') or filename: 'C:\\Users\x08_zz\\Desktop\\my_file'
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
scheduled_data_list=open(os.path.expandvars(file_path),“r”).readlines()
IOError:[Errno 22]无效的模式('r')或文件名:“C:\\Users\x08_zz\\Desktop\\my_file”

我将字符串转换为原始字符串的解决方案(仅适用于以下序列:'\a'、\b'、'\f'、'\n'、'\r'、'\t'、'\v'。所有转义序列的列表如下所示:

演示:

一旦在流程中创建了字符串,就不存在“原始字符串”这样的东西。指定字符串的
r”
方法仅存在于源代码本身中

这意味着
“\x01”
将创建一个由一个字节组成的字符串
0x01
,但
r“\x01”
将创建一个由4个字节组成的字符串
“0x5c”、“0x78”、“0x30”、“0x31”
。(假设我们讨论的是python 2,暂时忽略编码)

您在评论中提到,您正在从用户处获取字符串(gui或控制台输入在这里的工作方式相同)-在这种情况下,将不会处理字符串转义,因此您无需对此进行任何处理。您可以像这样轻松地检查它(或者不管windows的等价物是什么,我只说*nix):

%cat>测试EOF
%
ndpu的解决方案适合我

我无法抗拒增强它的诱惑(使它与古老的Python 2版本兼容,并希望加快它的速度):


我做了一次仔细的时间测试,结果证明ndpu的原始代码要快一点。列表理解速度很快,但生成器表达式速度更快。

为什么在错误消息中将“b_zz”替换为“x08_zz”?这就是我想知道的“ord('\b')”是8。要么将反斜杠加倍,要么在代码中用“r”作为字符串的前缀。但是,我从GUI输入中获取路径字符串。如何将“r”添加到开头?用户要问的是,如何获取一个未知字符串并使其在内存中不以二进制形式表示(一个“原始”字符串而不是解释的字符串),因为内存中没有原始字符串。原始字符串只是源代码的助手。如果通过(GUI)输入获取字符串,一切正常。@alwbtc如何从用户获取路径字符串?如果你在那里得到一个\b字符,我认为你没有从他们那里得到你想要的。你会得到用户提供的东西。仅查看/使用值时,不会发生字符串转换/取消跳过。“\b”部分与字符串本身无关,而是源代码解析的工件。
def str_to_raw(s):
    raw_map = {8:r'\b', 7:r'\a', 12:r'\f', 10:r'\n', 13:r'\r', 9:r'\t', 11:r'\v'}
    return r''.join(i if ord(i) > 32 else raw_map.get(ord(i), i) for i in s)
>>> file_path = "C:\Users\b_zz\Desktop\fy_file"
>>> file_path
'C:\\Users\x08_zz\\Desktop\x0cy_file'
>>> str_to_raw(file_path)
'C:\\Users\\b_zz\\Desktop\\fy_file'
% cat > test <<EOF                                             
heredoc> \x41
heredoc> EOF
% < test python -c "import sys; print sys.stdin.read()"
\x41
_dRawMap = {8:r'\b', 7:r'\a', 12:r'\f', 10:r'\n', 13:r'\r', 9:r'\t', 11:r'\v'}

def getRawGotStr(s):
    #
    return r''.join( [ _dRawMap.get( ord(c), c ) for c in s ] )