<p>我尝试用88行填充一个.txt文件,每行有两个字符,用空格隔开,将每行的第一个字符复制到list#1中,将每个列表的第二个字符复制到list#2中,然后用这两个列表填充字典。但是,当我试图将文件中的数据复制到列表中时,出现了一些问题。你能告诉我我什么地方做得不对吗?在</p>
<p>我总是在输入“column1[count]=readit[0]的那一行出现这样的错误:“IndexError:string index out out range”</p>
<pre><code>def main():
modo = open('codes.txt', 'r') #opening file
filezise = 0 #init'ing counter
for line in modo:
filezise+=1 #counting lines in the file
column1 = []*filezise
column2 = []*filezise #making the lists as large as the file
count = 0 #init'ing next counter
while count < filezise+1:
readit = str(modo.readline())
column1[count] = readit[0] ##looping through the file and
column2[count] = readit[2] ##populating the first list with the
count+=1 #first character and the second list
print(column1, column2) #with the second character
index = 0
n = 0
codebook = {} #init'ing dictionary
for index, n in enumerate(column1): #looping through to bind the key
codebook[n] = column2[index] #to its concordant value
print(codebook)
main()
</code></pre>