ValueError:索引55处不支持格式字符“a”(0x61),URL字符串为

2024-09-24 00:31:00 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试使用以下代码将URL和主机名发送到数据库:

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旁边的A。我试着再加上一个百分比,但还是没有影响。不太明白我为什么会犯这个错误。在


Tags: 代码数据库urldefconnectionfilenamequerycursor
1条回答
网友
1楼 · 发布于 2024-09-24 00:31:00

%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)

相关问题 更多 >