Python错误:“字符串索引超出范围”

时间:2019-09-04 23:56:05

标签: python excel

此代码可正常运行,但会出现此错误:

ws.write(row_idx, 148, row[496])
IndexError: string index out of range

在执行此行之前,代码运行正常,并且在添加代码时会出现此错误。

请,我不想在运行代码时对结果进行任何更改,只想解决此问题!

如何解决此错误?

import xlwt
import io
book = xlwt.Workbook()
ws = book.add_sheet('First Sheet')  # Add a sheet
with io.open("data.txt", mode="r", encoding="utf-8") as f:
for row_idx, row in enumerate(f):
row = row.rstrip() 

    ws.write(row_idx, 0, row[0:8])
    ws.write(row_idx, 1, row[9:19])
    ws.write(row_idx, 2, row[20:21])
    ws.write(row_idx, 3, row[22:24])
    ws.write(row_idx, 4, row[25:26])
    ws.write(row_idx, 5, row[27:28])
    ws.write(row_idx, 6, row[29:32])
    ws.write(row_idx, 7, row[33:37])
    ws.write(row_idx, 8, row[38:40])
    ws.write(row_idx, 9, row[41:43])
    ws.write(row_idx, 10, row[44:46])
    ws.write(row_idx, 11, row[47:51])
    ws.write(row_idx, 12, row[52:56])
    ws.write(row_idx, 13, row[57:58])
    ws.write(row_idx, 14, row[59:63])
    ws.write(row_idx, 15, row[64:65])
    ws.write(row_idx, 16, row[66:70])
    ws.write(row_idx, 17, row[71:72])
    ws.write(row_idx, 18, row[73:74])
    ws.write(row_idx, 19, row[75:76])
    ws.write(row_idx, 20, row[77:78])
    ws.write(row_idx, 21, row[79:82])
    ws.write(row_idx, 22, row[83:85])
    ws.write(row_idx, 23, row[86:88])
    ws.write(row_idx, 24, row[89:90])
    ws.write(row_idx, 25, row[91:93])
    ws.write(row_idx, 26, row[94:96])
    ws.write(row_idx, 27, row[97:110])
    ws.write(row_idx, 28, row[111:113])
    ws.write(row_idx, 29, row[114:118])
    ws.write(row_idx, 30, row[119:120])
    ws.write(row_idx, 31, row[121:122])
    ws.write(row_idx, 32, row[123:124])
    ws.write(row_idx, 33, row[125:126])
    ws.write(row_idx, 34, row[127:130])
    ws.write(row_idx, 35, row[131:134])
    ws.write(row_idx, 36, row[135:138])
    ws.write(row_idx, 37, row[139:141])
    ws.write(row_idx, 38, row[142:145])
    ws.write(row_idx, 39, row[146:149])
    ws.write(row_idx, 40, row[150:153])
    ws.write(row_idx, 41, row[154:157])
    ws.write(row_idx, 42, row[158:161])
    ws.write(row_idx, 43, row[162:165])
    ws.write(row_idx, 44, row[166:169])
    ws.write(row_idx, 45, row[170:172])
    ws.write(row_idx, 46, row[173:175])
    ws.write(row_idx, 47, row[176:178])
    ws.write(row_idx, 48, row[179:181])
    ws.write(row_idx, 49, row[182:183])
    ws.write(row_idx, 50, row[184:185])
    ws.write(row_idx, 51, row[186:188])
    ws.write(row_idx, 52, row[189:195])
    ws.write(row_idx, 53, row[196:202])
    ws.write(row_idx, 54, row[203:209])
    ws.write(row_idx, 55, row[210:212])
    ws.write(row_idx, 56, row[213:214])
    ws.write(row_idx, 57, row[215:216])
    ws.write(row_idx, 58, row[217:219])
    ws.write(row_idx, 59, row[220])
    ws.write(row_idx, 60, row[221:222])
    ws.write(row_idx, 61, row[223:225])
    ws.write(row_idx, 62, row[226:228])
    ws.write(row_idx, 63, row[229:230])
    ws.write(row_idx, 64, row[231:232])
    ws.write(row_idx, 65, row[233:234])
    ws.write(row_idx, 66, row[235:237])
    ws.write(row_idx, 67, row[238:350])
    ws.write(row_idx, 68, row[251:252])
    ws.write(row_idx, 69, row[253:255])
    ws.write(row_idx, 70, row[256:260])
    ws.write(row_idx, 71, row[261:266])
    ws.write(row_idx, 72, row[267:271])
    ws.write(row_idx, 73, row[272:276])
    ws.write(row_idx, 74, row[277:282])
    ws.write(row_idx, 75, row[283:286])
    ws.write(row_idx, 76, row[287:290])
    ws.write(row_idx, 77, row[291:292])
    ws.write(row_idx, 78, row[293])
    ws.write(row_idx, 79, row[294:296])
    ws.write(row_idx, 80, row[297:299])
    ws.write(row_idx, 81, row[300:303])
    ws.write(row_idx, 82, row[304:305])
    ws.write(row_idx, 83, row[306:307])
    ws.write(row_idx, 84, row[308:309])
    ws.write(row_idx, 85, row[310:311])
    ws.write(row_idx, 86, row[312:314])
    ws.write(row_idx, 87, row[315:317])
    ws.write(row_idx, 88, row[318:319])
    ws.write(row_idx, 89, row[320:321])
    ws.write(row_idx, 90, row[322:323])
    ws.write(row_idx, 91, row[324:325])
    ws.write(row_idx, 92, row[326:331])
    ws.write(row_idx, 93, row[332:335])
    ws.write(row_idx, 94, row[336:341])
    ws.write(row_idx, 95, row[342:347])
    ws.write(row_idx, 96, row[348:349])
    ws.write(row_idx, 97, row[350:351])
    ws.write(row_idx, 98, row[352:353])
    ws.write(row_idx, 99, row[354:356])
    ws.write(row_idx, 100, row[357:359])
    ws.write(row_idx, 101, row[360:361])
    ws.write(row_idx, 102, row[362:363])
    ws.write(row_idx, 103, row[364:365])
    ws.write(row_idx, 104, row[366:367])
    ws.write(row_idx, 105, row[368:369])
    ws.write(row_idx, 106, row[370:371])
    ws.write(row_idx, 107, row[372:373])
    ws.write(row_idx, 108, row[374:375])
    ws.write(row_idx, 109, row[376:378])
    ws.write(row_idx, 110, row[379:382])
    ws.write(row_idx, 111, row[383:386])
    ws.write(row_idx, 112, row[387:390])
    ws.write(row_idx, 113, row[391:394])
    ws.write(row_idx, 114, row[395:398])
    ws.write(row_idx, 115, row[399:402])
    ws.write(row_idx, 116, row[403:404])
    ws.write(row_idx, 117, row[405:409])
    ws.write(row_idx, 118, row[410:411])
    ws.write(row_idx, 119, row[412:416])
    ws.write(row_idx, 120, row[417:418])
    ws.write(row_idx, 121, row[419:422])
    ws.write(row_idx, 122, row[423:426])
    ws.write(row_idx, 123, row[427:430])
    ws.write(row_idx, 124, row[431:434])
    ws.write(row_idx, 125, row[435:437])
    ws.write(row_idx, 126, row[438:440])
    ws.write(row_idx, 127, row[441:443])
    ws.write(row_idx, 128, row[444:446])
    ws.write(row_idx, 129, row[447:450])
    ws.write(row_idx, 130, row[451:454])
    ws.write(row_idx, 131, row[455:458])
    ws.write(row_idx, 132, row[459:460])
    ws.write(row_idx, 133, row[461:462])
    ws.write(row_idx, 134, row[463:467])
    ws.write(row_idx, 135, row[468:469])
    ws.write(row_idx, 136, row[470:471])
    ws.write(row_idx, 137, row[472:473])
    ws.write(row_idx, 138, row[474:475])
    ws.write(row_idx, 139, row[476:477])
    ws.write(row_idx, 140, row[478:480])
    ws.write(row_idx, 141, row[481])
    ws.write(row_idx, 142, row[482:484])
    ws.write(row_idx, 143, row[485])
    ws.write(row_idx, 144, row[486:488])
    ws.write(row_idx, 145, row[489])
    ws.write(row_idx, 146, row[490:492])
    ws.write(row_idx, 147, row[493:495])
    ws.write(row_idx, 148, row[496])#string index out fo range!
    ws.write(row_idx, 149, row[497:500])
    ws.write(row_idx, 150, row[501:506])
    ws.write(row_idx, 151, row[507:512])
    ws.write(row_idx, 152, row[513:518])
    ws.write(row_idx, 153, row[519:524])
    ws.write(row_idx, 154, row[525:526])
    ws.write(row_idx, 155, row[527:528])
    ws.write(row_idx, 156, row[529:530])
    ws.write(row_idx, 157, row[531:533])
    ws.write(row_idx, 158, row[534:535])
    ws.write(row_idx, 159, row[536:537])

book.save("newconvert.xls")

1 个答案:

答案 0 :(得分:1)

您会收到此错误,因为字符串row不够长,即没有索引496

其余各行正常运行的原因是,切片不会在越界索引上引发错误,它只会返回一个空列表:

>>> a = "1234"
>>> a[4:]
''
>>> a[4]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range