繁体   English   中英

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

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

在 python3 代码下运行时出现以下错误。 输入 CSV 文件有 2 列,我必须将它们加载到 oracle 表中。

错误一:

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

错误2:

IndexError: array index out of range

代码:

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()

在第 14 行,"Not Available(") <= this is missing

另请查看第 15 行,其中也有一些误用的语音标记(反引号不能在 Python 中包含字符串)。

我希望这是有帮助的。

我修复了正确代码格式中的一些错误,您混淆了引号和双引号,以及执行比较运算符时的错误。 在拆分发生后,您还忘记用列表替换行。

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()

在 python if 语句中,需要这样写;

if lines[0] == "":

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

暂无
暂无

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

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