gpt4 book ai didi

python - 类型错误 : first argument must be string or compiled pattern

转载 作者:行者123 更新时间:2023-11-29 17:47:55 24 4
gpt4 key购买 nike

我正在从名为blocked_sites 的数据库表中获取值。如果表 Blocked_sites 中的第 0 个属性的值出现在文件 items.csv 的第 19 或第 26 字段中,则 csv 的该行将从 csv 文件中排除。我正在为此编写代码并收到此错误:

$ python csv_dupli_prev.py
Traceback (most recent call last):
File "csv_dupli_prev.py", line 48, in <module>
found = re.search(row[0], row1[19])
File "/home/debarati/anaconda3/lib/python3.6/re.py", line 182, in search
return _compile(pattern, flags).search(string)
File "/home/debarati/anaconda3/lib/python3.6/re.py", line 300, in _compile
raise TypeError("first argument must be string or compiled pattern")
TypeError: first argument must be string or compiled pattern

代码如下:

connection = pymysql.connect (host = "localhost", user = "root", passwd = "......", db = "city_details")          
cursor = connection.cursor ()
csv_file = csv.reader(open("items.csv", "r"))
newrows = []
cursor.execute ("select * from blocked_sites")
data4 = cursor.fetchall ()
for row in data4:
for row1 in csv_file:
str1 = row1[19]
str2 = row1[26]
found = re.search(row[0], str1)
found1 = re.search(row[0], str2)
if found==None and found1==None and row1 not in newrows:
newrows.append(row1)
writer = csv.writer(open("items.csv", "w"))
writer.writerows(newrows)

最佳答案

我在代码中更改了以下行:

cursor.execute ("select * from blocked_sites")

对此:

cursor.execute ("select content from blocked_sites")

并且该错误已得到修复。

关于python - 类型错误 : first argument must be string or compiled pattern,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49653199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com