[英]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.