如何索引文本文件中的每个字符?
How do I index through each character in a text file?
我有这个文件:
01000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
1000000000
01100000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
1000000000
我想遍历每个字符并基本上将它们放在一个二维列表中,其中内部列表包含每个字符,外部列表包含每个字符的列表并以 \n\n
字符结束,所以在这里我将有一个 2 x 8*8+10 的列表。问题是在每一行之后是 \n
字符,它也被视为一个字符,而且,我已经尝试使用 read()
函数来完成它,它遍历我的列表,但对于一些原因是它得到了我作为参数给出的字符数,而不是它们的索引。它还以 ''
字符开头,我猜它会将我作为参数给出的项目数加起来,并将不同的字符放在下一个列表中。
这是我的代码:
lines, columns=8, 8
bias=10
x=[[0 for i in range(lines*columns)] for j in range(patternCount)]
with open("sabloane.txt", "r") as file:
for pattern in range(patternCount):
for pixel in range(lines*columns+bias):
x[sablon][pixel]=file.read(pattern*patternCount+pixel)
print(x)
它returns:
[['', '0', '10', '000', '00\n0', '00000', '00\n000', '00000\n0', '0000000\n', '00000000\n', '00000000\n0', '0000000\n000', '00000\n100000', '0000\n\n0110000', '0\n00000000\n000', '00000\n00000000\n', '00000000\n0000000', '0\n00000000\n000000', '00\n1000000000\n\n', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']]
别想的太复杂了。
# Assume you got the file string to variable string
outer = []
items = string.split('\n\n')
for item in items:
inner = []
temp = ''.join(item.split('\n'))
for c in temp:
inner.append(c)
outer.append(inner)
好吧,对于任何对我之前关于从文件中获取第 N 个元素的评论感兴趣的人,我刚刚编辑了@Hanxi Fu 的代码,以便将前 N-1 行的元素放入一个矩阵,然后从最后一行到另一个矩阵的其他元素。
x=[[0 for i in range(lines*columns)] for j in range(patternCount)]
y=[[0 for i in range(bias)] for j in range(patternCount)]
with open("sabloane.txt", "r") as file:
stringFile=file.read()
patterns=stringFile.split("\n\n")
patterns.pop(patternCount)
for i in range(patternCount):
pixel="".join(patterns[i].split("\n"))
for j in range(lines*columns):
x[i][j]=pixel[j]
for j in range(bias):
y[i][j]=pixel[lines*columns+j]
我有这个文件:
01000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
1000000000
01100000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
1000000000
我想遍历每个字符并基本上将它们放在一个二维列表中,其中内部列表包含每个字符,外部列表包含每个字符的列表并以 \n\n
字符结束,所以在这里我将有一个 2 x 8*8+10 的列表。问题是在每一行之后是 \n
字符,它也被视为一个字符,而且,我已经尝试使用 read()
函数来完成它,它遍历我的列表,但对于一些原因是它得到了我作为参数给出的字符数,而不是它们的索引。它还以 ''
字符开头,我猜它会将我作为参数给出的项目数加起来,并将不同的字符放在下一个列表中。
这是我的代码:
lines, columns=8, 8
bias=10
x=[[0 for i in range(lines*columns)] for j in range(patternCount)]
with open("sabloane.txt", "r") as file:
for pattern in range(patternCount):
for pixel in range(lines*columns+bias):
x[sablon][pixel]=file.read(pattern*patternCount+pixel)
print(x)
它returns:
[['', '0', '10', '000', '00\n0', '00000', '00\n000', '00000\n0', '0000000\n', '00000000\n', '00000000\n0', '0000000\n000', '00000\n100000', '0000\n\n0110000', '0\n00000000\n000', '00000\n00000000\n', '00000000\n0000000', '0\n00000000\n000000', '00\n1000000000\n\n', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']]
别想的太复杂了。
# Assume you got the file string to variable string
outer = []
items = string.split('\n\n')
for item in items:
inner = []
temp = ''.join(item.split('\n'))
for c in temp:
inner.append(c)
outer.append(inner)
好吧,对于任何对我之前关于从文件中获取第 N 个元素的评论感兴趣的人,我刚刚编辑了@Hanxi Fu 的代码,以便将前 N-1 行的元素放入一个矩阵,然后从最后一行到另一个矩阵的其他元素。
x=[[0 for i in range(lines*columns)] for j in range(patternCount)]
y=[[0 for i in range(bias)] for j in range(patternCount)]
with open("sabloane.txt", "r") as file:
stringFile=file.read()
patterns=stringFile.split("\n\n")
patterns.pop(patternCount)
for i in range(patternCount):
pixel="".join(patterns[i].split("\n"))
for j in range(lines*columns):
x[i][j]=pixel[j]
for j in range(bias):
y[i][j]=pixel[lines*columns+j]