我从名为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)
答案 0 :(得分:0)
我在代码中更改了以下行:
cursor.execute ("select * from blocked_sites")
到此:
cursor.execute ("select content from blocked_sites")
并修复了错误。