I am trying to read the data from "data.txt" and write it to SQLITE database, what I actually get is only first row of the record. I figured out by printing the pandas frame object, it does get all the data from "txt" but failed to iterate throughout, to me the iteration loop looks fine can anybody figure out what I am doing wrong here which making it fail to replicate all the rows of file.
File data looks like this:
862 1
4828 1
6429 1
10013 1
7729 1
380 1
3808 1
7246 1
1663 1
Secondly, you will notice that spaces between the columns are inconsistent, at some places there are spaces and at some places these are tab separated. I have few thousands of inconsistent rows , this is just a subset of those for reference. Is there any way to handle this?
I tweaked delimiter parameter and also split function but it doesn't workout here. Any suggestion?
Actual results:
Only one row into DB
862 1
Expected results:
862 1
4828 1
6429 1
10013 1
7729 1
380 1
3808 1
7246 1
1663 1
Here is the piece of code I tried:
data = pd.read_csv ("data.txt")
rows = pd.DataFrame(data)
#print (rows)
for row in rows:
r = row.split()
object_id = r[0]
object_type = r[1]
cur.execute(''' INSERT INTO objects (object_id, object_type)
VALUES (?, ?) ''', (object_id, object_type))
conn.commit()
Any valuable input is appreciated! Thanks in advance.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…