简体   繁体   中英

Can't figure out the cause of this list error

I am trying to read this text file into a potsgres table (skipping first 6 rows). Although I can print the result set to the console as list of lists, I don't understand how to fix this.

$cat test.plt
Geolife trajectory
WGS 84
Altitude is in Feet
Reserved 3
0,2,255,My Track,0,0,2,8421376
0
39.9756783,116.3308383,0,131.2,39717.4473148148,2008-09-26,10:44:08
39.9756649,116.3308749,0,131.2,39717.4473842593,2008-09-26,10:44:14
39.97564,116.3308749,0,131.2,39717.4474189815,2008-09-26,10:44:17
39.9756533,116.3308583,0,131.2,39717.4474537037,2008-09-26,10:44:20
39.9756316,116.3308299,0,131.2,39717.4474884259,2008-09-26,10:44:23
39.9753166,116.3306299,0,131.2,39717.4480324074,2008-09-26,10:45:10
39.9753566,116.3305916,0,131.2,39717.4480671296,2008-09-26,10:45:13
39.9753516,116.3305249,0,131.2,39717.4481018518,2008-09-26,10:45:16

My script:

import psycopg2

# Try to connect
query = "INSERT INTO dummy (lat, lon, flag, alt, passeddate, gpsdate, gpstime) VALUES (%s, %s, %s, %s, %s, %s, %s)"


try:
    conn=psycopg2.connect("dbname='mydb' user='myuser'  host='host_ip' password='mypass'")
except:
    print("I am unable to connect to the database.")

cur = conn.cursor()
try:

    # INSERRT data to the database
    with open('test.plt') as file: 
        file_content = file.readlines()[6:]
        values = [line.split() for line in file_content]

        cur.executemany(query, values)

except (Exception, psycopg2.DatabaseError) as error:
    print(error)

Output:

$ python dummy.py 
list index out of range

How do I fix this?

EDIT Output without using try statement

Traceback (most recent call last):
  File "dummy.py", line 20, in <module>
    cur.executemany(query, values)
IndexError: list index out of range

EDIT-2

I can however print the result set to the console with

with open('test.plt') as file: # 
        file_content = file.readlines()[6:]
        values = [line.split() for line in file_content]
        print(values)
        cur.executemany(query, values)
[['39.9756783,116.3308383,0,131.2,39717.4473148148,2008-09-26,10:44:08'], ['39.9756649,116.3308749,0,131.2,39717.4473842593,2008-09-26,10:44:14'], ['39.97564,116.3308749,0,131.2,39717.4474189815,2008-09-26,10:44:17'], ['39.9756533,116.3308583,0,131.2,39717.4474537037,2008-09-26,10:44:20'], ['39.9756316,116.3308299,0,131.2,39717.4474884259,2008-09-26,10:44:23'], ['39.9753166,116.3306299,0,131.2,39717.4480324074,2008-09-26,10:45:10'], ['39.9753566,116.3305916,0,131.2,39717.4480671296,2008-09-26,10:45:13'], ['39.9753516,116.3305249,0,131.2,39717.4481018518,2008-09-26,10:45:16']]
list index out of range

I think the value assignment should look like this, yes?: values = [line[0].split(",").strip() for line in file_content]

It appears changing split() to split(',') solves the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM