簡體   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