简体   繁体   English

Python 代码错误:缩进中制表符和空格的使用不一致和数组索引超出范围

[英]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.在 python3 代码下运行时出现以下错误。 Input CSV file has 2 columns which I have to load into oracle table.输入 CSV 文件有 2 列,我必须将它们加载到 oracle 表中。

Error 1:错误一:

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

Error 2:错误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在第 14 行,"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).另请查看第 15 行,其中也有一些误用的语音标记(反引号不能在 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;在 python if 语句中,需要这样写;

if lines[0] == "":

This might help you: https://realpython.com/python-conditional-statements/这可能会对您有所帮助: https://realpython.com/python-conditional-statements/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TabError:缩进错误中制表符和空格的使用不一致 - TabError: inconsistent use of tabs and spaces in indentation Error “缩进中的制表符和空格使用不一致” - “inconsistent use of tabs and spaces in indentation” “缩进中制表符和空格的使用不一致” - "inconsistent use of tabs and spaces in indentation" 缩进中制表符和空格的使用不一致 - Inconsistent use of tabs and spaces in indentation 在缩进notepad ++ Python中不一致地使用制表符和空格 - inconsistent use of tabs and spaces in indentation notepad++ Python 缩进python3.8中制表符和空格的不一致使用 - inconsistent use of tabs and spaces in indentation python3.8 如何在缩进中执行不一致的制表符和空格使用? | PYTHON - How to execute inconsistent use of tabs and spaces in indentation? | PYTHON 即使在 Visual Studio 代码中从空格转换为制表符后,仍收到制表符错误“缩进中制表符和空格的使用不一致” - Receiving tab error “inconsistent use of tabs and spaces in indentation” even after converting from spaces to tabs in visual studio code Pycharm TabError:在缩进中使用不一致的制表符和空格 - Pycharm TabError: inconsistent use of tabs and spaces in indentation 什么是缩进中制表符和空格的不一致使用? - What is inconsistent use of tabs and spaces in indentation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM