Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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 3.x 如何统一路径输入_Python 3.x - Fatal编程技术网

Python 3.x 如何统一路径输入

Python 3.x 如何统一路径输入,python-3.x,Python 3.x,我使用以下代码获取excel文件的路径: xlsx_path = input("excel path:") print(repr(xlsx_path)) 用户将给我“C:\Users\Lenovo\test.xlsx”或C:\Users\Lenovo\test.xlsx。但是如果用户给我一个“C:\Users\Lenovo\test.xlsx”(注意用户会给我一个多余的引号),那么xlsx\u路径实际上将是““C:\\Users\\Lenovo\\test.xlsx”。那我就认不出路了。如何获

我使用以下代码获取excel文件的路径:

xlsx_path = input("excel path:")
print(repr(xlsx_path))

用户将给我
“C:\Users\Lenovo\test.xlsx”
C:\Users\Lenovo\test.xlsx
。但是如果用户给我一个
“C:\Users\Lenovo\test.xlsx”
(注意用户会给我一个多余的引号),那么
xlsx\u路径实际上将是
““C:\\Users\\Lenovo\\test.xlsx”
。那我就认不出路了。如何获得统一的路径,而不考虑用户是否提供任何路径?

如果用户在文件名周围键入双引号,则可以使用字符串方法将其删除。如果没有双引号,它将没有任何效果。试试这个:

xlsx_path = input("excel path:")
xlsx_path = xlsx_path.strip('"')
print(repr(xlsx_path))

_path=the_path.strip(“\”)适用于两种情况,给出相同的结果。这是否回答了您的问题?