如何在python中转换uri

时间:2017-02-24 07:37:47

标签: python python-2.7 python-3.x

我需要通过uri转换python

D:\*****\******\******\testing.txt 

file:///D:/*****/******/*****/testing.txt

我该怎么办?谢谢

我编辑因为我错了最终的uri,对不起

2 个答案:

答案 0 :(得分:1)

在HarryCBurn所做的基础上,斜线也需要改变。

path = r"D:\*****\******\******\testing.txt"   #the original string
path = "file:///" + path                       #text concatenation
path = path.replace("\\","/")                  #changing the slashes
print path

如果您想以不同的方式处理某些字符串 - 例如那些以" htt"开头的字符串。然后这可以工作

path1 = r"D:\*****\******\******\testing.txt"   #the original string
path2 = r"http: // *****"

def pathedit(path):
    if path.startswith("http"):                       #checks if the list starts with htt
        return path                             #Returns value
    path = "file:///" + path                #text concatenation
    path = path.replace("\\","/")           #changing the slashes
    return path                             #Returns value

print pathedit(path1)
print pathedit(path2)

答案 1 :(得分:0)

只是一个字符串连接:

text = "your line here"
text = "file:///" + text