基本上我需要通过char读取文件char,我需要知道该字符是哪一行。所以它是一个循环内的循环。
keyboard = open(default_layout, 'r')
with open(default_layout) as l:
for line in l:
queue=0
#i=i+1
while queue != max_chars_per_key:
c = l.read(1)
<...>
if (queue==0):
key_name=c
key[c] = [key_name, queue+1]
<...>
queue+=1
<...bunch of code...>
问题是,在传递参数后使用此代码,它给了我:
ValueError:混合迭代和读取方法会丢失数据
关于变通办法的任何想法?
答案 0 :(得分:1)
获得所需内容的最简单方法是:
with open(...) as fh:
for line_num, line in enumerate(fh):
for col_num, char in enumerate(line):
...
要将其与您的代码放在上下文中,大概是:
keyboard = open(default_layout, 'r')
with open(default_layout) as l:
for line in l:
queue = 0
for c in line[:max_chars_per_key]:
...
if queue == 0:
key_name=c
key[c] = [key_name, queue+1]
...
queue += 1
...