+ -
当前位置:首页 → 问答吧 → 求代码中标出颜色段的具体解释,谢谢

求代码中标出颜色段的具体解释,谢谢

时间:2011-01-18

来源:互联网

本帖最后由 predatorymh 于 2011-01-18 10:09 编辑

#-*-coding:cp936-*-
from Txt_read_write import Txt_read_write
infilelocalObject=Txt_read_write()#use local data for dictionary
infilelocalObject.ReadTxt('采出的.txt')
dictionary={}
for a in infilelocalObject.List:
    dictionary[a[0]]=a#
ChangeObject=Txt_read_write()
ChangeObject.ReadTxt('local20110105.txt')
resultList=[]
for line in ChangeObject.List:
    for x in range(10):
        line+=['']        
   if dictionary.get(line[0]):#
        resultList+=[line[:9]+dictionary.get(line[0])]#
    else:
        resultList+=[line]
ChangeObject.WriteTxt(resultList,'resultList.txt')
raw_input('finished')

作者: predatorymh   发布时间: 2011-01-18

回复 predatorymh


    >>> line=['']#txt 文件里,有些是空行,就为形成,列表 只有一个元素
>>> line
['']
>>> line[9]#所以 像这种情况 索引就会超出范围

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    line[9]
IndexError: list index out of range
>>> for x in range(10):# 在这里加了元素后,程序索引就不会出错
        line+=['']

        
>>> line
['', '', '', '', '', '', '', '', '', '', '']
>>> line[9]
''
>>>

>>>  if dictionary.get(line[0]):#
        resultList+=[line[:9]+dictionary.get(line[0])]#

伪代码是:
  如果 字典.得到(行里的第一个元素):
         结果的表 就加上 [行里的第一个元素到第九个元素 加上 字典.得到(行里的第一个元素)]

作者: wqjwftcaqr   发布时间: 2011-01-18