我有一個CSV,看起來是這樣的:python - 列表索引超出範圍,使用CSV?
F02303521,"Smith,Andy",GHI,"Smith,Andy",GHI,,,
F04300621,"Parker,Helen",CERT,"Yu,Betty",IOUS,,,
我想刪除所有在第二列等於第4列線(例如當Smith,Andy = Smith,Andy
)。我嘗試用"
作爲分隔符和分裂列到這樣做在python:
F02303521,
Smith,Andy
,GHI,
Smith,Andy
,GHI,,,
我想這個Python代碼:
testCSV = 'test.csv'
deletionText = 'linestodelete.txt'
correct = 'correctone.csv'
i = 0
j = 0 #where i & j keep track of line number
with open(deletionText,'w') as outfile:
with open(testCSV, 'r') as csv:
for line in csv:
i = i + 1 #on the first line, i will equal 1.
PI = line.split('"')[1]
investigator = line.split('"')[3]
#if they equal each other, write that line number into the text file
as to be deleted.
if PI == investigator:
outfile.write(i)
#From the TXT, create a list of line numbers you do not want to include in output
with open(deletionText, 'r') as txt:
lines_to_be_removed_list = []
# for each line number in the TXT
# remove the return character at the end of line
# and add the line number to list domains-to-be-removed list
for lineNum in txt:
lineNum = lineNum.rstrip()
lines_to_be_removed_list.append(lineNum)
with open(correct, 'w') as outfile:
with open(deletionText, 'r') as csv:
# for each line in csv
# extract the line number
for line in csv:
j = j + 1 # so for the first line, the line number will be 1
# if csv line number is not in lines-to-be-removed list,
# then write that to outfile
if (j not in lines_to_be_removed_list):
outfile.write(line)
但這一行:
PI = line.split('"')[1]
我得到:
Traceback (most recent call last): File "C:/Users/sskadamb/PycharmProjects/vastDeleteLine/manipulation.py", line 11, in PI = line.split('"')[1] IndexError: list index out of range
,我認爲這會做PI = Smith,Andy
investigator = Smith,Andy
......爲什麼這不會發生呢?
任何幫助將不勝感激,謝謝!
這意味着有在'list'少於兩個元素。把它放在一個'try'塊中,並且有匹配的'except'輸出'line.split(''')' – TigerhawkT3
你有沒有隨機的空行?另外,爲什麼不使用內置的函數?在csv模塊中? – NightShadeQueen
爲什麼不使用非常好的'csv'模塊? –