代码之家  ›  专栏  ›  技术社区  ›  Masoud Rahimi Chris

包含特殊字符的字符串的绝对路径

  •  1
  • Masoud Rahimi Chris  · 技术社区  · 7 年前

    假设这条路径: C:\Files\2c2b2541\00025\test.x

    重要的 pyodbc . 当我尝试将其转换为绝对路径时,会出现以下错误:

    我也试着替换 "\" 具有 "/"

    import os
    
    # path = cursor.execute(query, "some_input").fetchone()[0]
    path = 'C:\Files\2c2b2541\00025\test.x'
    
    print(os.path.abspath(path))
    
    4 回复  |  直到 7 年前
        1
  •  1
  •   Blckknght    7 年前

    从您对其他答案的评论来看,您使用的数据库中的数据似乎已经损坏。也就是说,您在那里存储了一个文本空字节,可能还有其他伪字节(如 \2 可能变成 \x02

    首先,您应该修复将值放入数据库的任何代码,这样它就不会再将虚假数据放入数据库。您还没有描述数据是如何进入数据库的,因此我无法为您提供太多关于如何做到这一点的指导。但是大多数编程语言(和DB库)都有工具来防止转义序列在不需要的字符串中进行求值。

    \0 空字节,带有 \\0 (或适用于DB的任何适当转义序列)。您可能需要查找特殊字符,如换行符( \n )和不可打印字符(如 \x02 )还有。

    如果您根本无法控制数据库,我只会尝试在输出端解决这个问题。

        2
  •  1
  •   hygull    7 年前

    我认为下面是解决你问题的正确方法。

    >>> def get_fixed_path(path):
    ...     path = repr(path)
    ...     path = path.replace("\\", "\\\\")
    ...     path = path.replace("\\x", "\\\\0")
    ...     path = os.path.abspath(path3).split("'")[1]
    ...     return path
    ...
    >>>
    >>> path = 'C:\Files\2c2b2541\00025\test.x'
    >>> path
    'C:\\Files\x02c2b2541\x0025\test.x'
    >>>
    >>> print(path)
    C:\Filesc2b2541 25     est.x
    >>>
    >>> final_path = get_fixed_path(path)
    >>> final_path
    'C:\\Files\\002c2b2541\\00025\\test.x'
    >>>
    >>> print(final_path)
    C:\Files\002c2b2541\00025\test.x
    >>>
    

    下面是上述解决方案中每个步骤/语句的详细说明。

    第一步(问题)
    >>> import os
    >>>
    >>> path = 'C:\Files\2c2b2541\00025\test.x'
    >>> path
    'C:\\Files\x02c2b2541\x0025\test.x'
    >>>
    >>> print(path)
    C:\Filesc2b2541 25     est.x
    >>>
    
    第二步(问题)
    >>> path2 = repr(path)
    >>> path2
    "'C:\\\\Files\\x02c2b2541\\x0025\\test.x'"
    >>>
    >>> print(path2)
    'C:\\Files\x02c2b2541\x0025\test.x'
    >>>
    
    第三步(问题)
    >>> path3 = path2.replace("\\", "\\\\")
    >>> path3
    "'C:\\\\\\\\Files\\\\x02c2b2541\\\\x0025\\\\test.x'"
    >>>
    >>> print(path3)
    'C:\\\\Files\\x02c2b2541\\x0025\\test.x'
    >>>
    >>> path3 = path3.replace("\\x", "\\\\0")
    >>> path3
    "'C:\\\\\\\\Files\\\\\\002c2b2541\\\\\\00025\\\\test.x'"
    >>>
    >>> print(path3)
    'C:\\\\Files\\\002c2b2541\\\00025\\test.x'
    >>>
    
    >>> os.path.abspath(path3)
    "C:\\Users\\RISHIKESH\\'C:\\Files\\002c2b2541\\00025\\test.x'"
    >>>
    >>> os.path.abspath(path2)
    "C:\\Users\\RISHIKESH\\'C:\\Files\\x02c2b2541\\x0025\\test.x'"
    >>>
    >>> os.path.abspath('k')
    'C:\\Users\\RISHIKESH\\k'
    >>>
    >>> os.path.abspath(path3).split("'")
    ['C:\\Users\\RISHIKESH\\', 'C:\\Files\\002c2b2541\\00025\\test.x', '']
    >>> os.path.abspath(path3).split("'")[1]
    'C:\\Files\\002c2b2541\\00025\\test.x'
    >>>
    
    最后一步(解决方案)
    >>> final_path = os.path.abspath(path3).split("'")[1]
    >>>
    >>> final_path
    'C:\\Files\\002c2b2541\\00025\\test.x'
    >>>
    >>> print(final_path)
    C:\Files\002c2b2541\00025\test.x
    >>>
    
        3
  •  0
  •   Lê Tư Thành    7 年前

    将“\”替换为“\ \”。

        4
  •  -1
  •   BernardL    7 年前

    \\

    import os
    path = r'C:\Files\2c2b2541\00025\test.x' #r before the string
    
    print(os.path.abspath(path))