2017-07-03 22 views
1

试图将URL和主机名发送到数据库使用下面的代码:ValueError异常:不支持的格式字符“A”(0x61)在与URL字符串指数55

def sendToDatabase(self, case, filename): 
103   ext = os.path.splitext(filename)[1] 
104   filenoext = filename.strip(ext) 
105   url = "https://apses4859.ms.ds.uhc.com:10943/rest/download/C%3A/IBM/ISA5/ISA5/isa/cases/%s/%s-analyzer_ISA_PD/%s_Leak_Suspects/index.html" % (case, filename,filenoext) 
106   cursor = connection.cursor() 
107   m = re.search(r"([^.]*)", filename) 
108   hostname = m.group(1) 
109   query = "INSERT INTO StoryData (hostName, reportName) VALUES (%s, %s)" 
110   cursor.execute(query , (hostname, url)) 
111   connection.commit() 
112   cursor.close() 

它不顺心的甲旁% 3A由于某种原因。我尝试添加额外的%,并且仍然不会影响它。不太明白为什么我得到这个错误。

+0

我试了一下,也和它为我的工作确定。 ('%% 3A'而不是'%3A') – MarianD

回答

1

%3A被解释为格式化字符串,并且没有A格式。更好地切换到新风格的格式,即。使用format方法,而不是%操作:

url = "https://apses4859.ms.ds.uhc.com:10943/rest/download/C%3A/IBM/ISA5/ISA5/isa/cases/{}/{}-analyzer_ISA_PD/{}_Leak_Suspects/index.html".format(case, filename,filenoext) 
相关问题