【问题标题】:ValueError: unsupported format character 'a' (0x61) at index 55 with URL stringValueError:不支持的格式字符“a”(0x61)在索引 55 处带有 URL 字符串
【发布时间】:2017-12-07 00:27:53
【问题描述】:

尝试使用以下代码将 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。我已经尝试在其中添加额外的 % ,但它仍然不会影响它。不太明白为什么我会收到这个错误。

【问题讨论】:

  • 我也试过了,对我来说效果很好。 (%%3A 而不是%3A

标签: python string url pymysql


【解决方案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)

【讨论】:

    猜你喜欢
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多