值错误:清楚时,项目不在列表中

时间:2015-01-04 05:43:03

标签: python string list indexing

我有一个列表,abbreviations,填充了字符串对象。我试图在列表中调用字符串的.index。当我用字符串调用.index方法时,我得到一个ValueError:' LING'当它清楚地在列表中时,它不在列表中。

enter image description here

我的代码:

for item in abbreviations:
    print item

print abbreviations.index("LING")

为什么' LING'清除时不存在?我添加了以下代码行,用于搜索'缩写'对于字符串的索引。我很困惑 - " LING"显然在我的缩写列表中。

编辑(附加代码):

import csv 

myfile = open("/Users/it/Desktop/Classbook/classAbrevs.csv", "rU")

lines = [tuple(row) for row in csv.reader(myfile)]

longSubjectNames = [] 
abbreviations = [] 

masterAbrevs = []
for item in lines:
    longSubjectNames.append(item[0])
    abbreviations.append(item[1])

with open ("/Users/it/Desktop/Classbook/masterClassList.txt", "r") as myfile:
    masterSchedule = tuple(open("/Users/it/Desktop/Classbook/masterClassList.txt", 'r'))

for masterline in masterSchedule:
    masterline.strip()
    masterSplitLine = masterline.split("|")

    subjectAbrev = ""
    if masterSplitLine[0] != "STATUS":
        subjectAbrev = ''.join([i for i in masterSplitLine[2] if not i.isdigit()])
        masterAbrevs.append(subjectAbrev)

finalAbrevs = []
for subject in masterAbrevs:
    if (subject[-1] == 'W') and (subject[-2:] != 'UW'): 
        subject = subject[:-1]
    finalAbrevs.append(subject)

x = 0

for item in abbreviations:
    print item

print abbreviations.index("LING")

for item in finalAbrevs:
    if masterSplitLine[0] != "STATUS":
        concat = abbreviations.index(str(finalAbrevs[x]).strip())
        print "The abbreviation for " + str(item) + " is: " + longSubjectNames[concat]
    x = x + 1

输出:



masterAbrevs = []
for item in lines:
    longSubjectNames.append(item[0])
    abbreviations.append(item[1])

print '-'.join(abbreviations)




是:



ACA-ACCY-AFST-AMST-ANAT-ANTH-APSC-ARAB-AH-FA-ASTR-BIOC-BISC-BME-BMSC-BIOS-BADM-CHEM-CHIN-CE-CLAS-CCAS-COMM-CSCI-CFA-CNSL-CPED-DNSC-EALL-ECON-EDUC-ECE-EHS-ENGL-EAP-EMSE-ENRP-EPID-EXSC-FILM-FINA-FORS-FREN-GEOG-GEOL-GER-GREK-HCS-HSCI-HLWL-HSML-HEBR-HIST-HOMP-HONR-HDEV-HOL-HSSJ-ISTM-IDIS-IAD-INTD-IAFF-IBUS-ITAL-JAPN-JSTD-KOR-LATN-LAW-LSPA-LING	-MGT-MKTG-MBAD-MATH-MAE-MED-MICR-MMED-MSTD-MUS-NSC-ORSC-PSTD-PERS-PHAR-PHIL-PT-PA-PHYS-PMGT-PPSY-PSC-PORT-PSMB-PSYD-PSYC-PUBH-PPPA-REL-SEAS-SMPA-SLAV-SOC-SPAN-SPED-SPHR-STAT-SMPP-SUST-TRDA-TSTD-TURK-UW-WLP-WSTU
Traceback (most recent call last):
  File "/Users/it/Desktop/Classbook/sortClasses.py", line 25, in <module>
    with open ("/Users/it/Desktop/Classbook/masterClassList.txt", "r") as anything:
IOError: [Errno 2] No such file or directory: '/Users/it/Desktop/Classbook/masterClassList.txt'
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

myfile = open("/Users/it/Desktop/Classbook/classAbrevs.csv", "rU")

lines = [tuple(row) for row in csv.reader(myfile)]

longSubjectNames = [] 
abbreviations = [] 

masterAbrevs = []
for item in lines:
    longSubjectNames.append(item[0])
    abbreviations.append(item[1])

with open ("/Users/it/Desktop/Classbook/masterClassList.txt", "r") as myfile:

问题出在这里;

with open ("/Users/it/Desktop/Classbook/masterClassList.txt", "r") as myfile:

您在之前定义了myfile

myfile = open("/Users/it/Desktop/Classbook/classAbrevs.csv", "rU")

实际上,abbreviations = []实际上没有从classAbrevs.csv获取数据。因为它正在使用此行定义masterClassList.txt时从myfile获取数据;

with open ("/Users/it/Desktop/Classbook/masterClassList.txt", "r") as myfile

这就是为什么你的字符串不在该列表中。也是这一行;

for item in lines:
    longSubjectNames.append(item[0])
    abbreviations.append(item[1])

您确定item[1]是否包含您想要的所有字符串?

  

我尝试了这些代码,我只是从你的抄袭中粘贴它,这就是结果;

enter image description here

答案 1 :(得分:0)

问题在于,您运行的结果是:

“LING \ t”会显示在您的列表中,而非“LING”

运行此操作后,我得到了所需的索引:

abbreviations.index("LING\t")

71

要纠正这个问题,有很多方法可以剥离 \ t ,我正在展示其中一个:

abbreviations.append(item[1].strip())

通过更正此行,您的项[1] 会删除 \ t ,然后再附加到缩写列表。