简体   繁体   中英

Python code error : inconsistent use of tabs and spaces in indentation and array index out of range

I am getting following error while running below python3 code. Input CSV file has 2 columns which I have to load into oracle table.

Error 1:

 File "csv_package_script_1.py", line 15
    if lines[0] = "":
                    ^
TabError: inconsistent use of tabs and spaces in indentation

Error 2:

IndexError: array index out of range

Code:

import cx_Oracle
import csv
import os 

INPUT_PATH = 'C:\\Users\\leorbts\\Projects\\Recon\\Python_Codes\\ITDate_Python\\data\\incoming\\'
infile     = 'Platform_List.csv'
data_file  = os.path.join(INPUT_PATH, infile)

with open(data_file, "r") as csv_file:
    csv_reader = csv.reader(csv_file, delimiter='|')
    for lines in csv_reader:
        lines.split(',')
        if lines[0] = "":
           lines[0] = "Not Available'
        if lines[1] = "":`enter code here`
           lines[1] = "Not Available'
        print(lines[0], lines[1])
        db_curr.execute("INSERT INTO CDC_STG_TBL(TechStakName, ProvisionDate) VALUES ( :1, :2 )", (lines[0], lines[1]))
db_curr.close()
db_conn_str.commit()
db_conn_str.close()

On Line 14, there is no ending speech mark to "Not Available(") <= this is missing

Also look at Line 15, there are some misused speech marks and there as well (back ticks are cannot enclose strings in Python).

I hope this is helpful.

i fixed some of the errors in proper code formatting, you had mixed up quotations and double quotations, along with mistakes in doing comparison operators. You also forgot to replace lines with the list after the split has occured.

import cx_Oracle
import csv
import os 

INPUT_PATH = 'C:\\Users\\leorbts\\Projects\\Recon\\Python_Codes\\ITDate_Python\\data\\incoming\\'
infile     = 'Platform_List.csv'
data_file  = os.path.join(INPUT_PATH, infile)

with open(data_file, "r") as csv_file:
    csv_reader = csv.reader(csv_file, delimiter='|')
    for lines in csv_reader:
        lines = lines.split(',')
        if lines[0] == "":
           lines[0] = "Not Available"
        if lines[1] == "":
           lines[1] = "Not Available"
        print(lines[0], lines[1])
        db_curr.execute("INSERT INTO CDC_STG_TBL(TechStakName, ProvisionDate) VALUES ( :1, :2 )", (lines[0], lines[1]))
db_curr.close()
db_conn_str.commit()
db_conn_str.close()

In python if statements, you need to write it like this;

if lines[0] == "":

This might help you: https://realpython.com/python-conditional-statements/

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