the contents of the txt document are as follows:
-
supplementary question. The code is improved as follows, but still encounter problems:
import mysql.connector
conn = mysql.connector.connect(host="localhost", port=3306, user="root", password="999", database="test", charset="utf8")
cursor = conn.cursor()
f = open("/Users/helen/Documents/dump.txt", mode = "r+")
while True:
line = f.readline()
if line:
line = line.split(",")
print(line)
id = line[0]
name = line[1]
cursor.execute("insert into user2(id, name) values(%s,%s)", [id, name])
else:
break
f.close()
cursor.close()
conn.commit()
conn.close()
error content:
["{\\rtf1\\ansi\\ansicpg936\\cocoartf1504\\cocoasubrtf830\n"]
Traceback (most recent call last):
File "/Users/helen/Documents/txt.py", line 16, in <module>
name = line[1]
IndexError: list index out of range