如何从 python 中的字符串中的特定位置读取一组字符
How do I read a set of characters from a specific position in a string in python
所以我正在编写自己的密码器和解码器来找点乐子,但我被困在我的解密程序中,我需要在我的字符串中向上移动 1 个字符才能解密下一个“块”字符数。
cyinput2 = ",#96c8a2: ,#808000: ,#96c8a2: ,#e1a95f: ,#808000: ,#6f00ff:"
#989E86
上面是我尝试向上移动的字符串,本质上我是在尝试从第一个 6 进制字符串移动到第二个字符串,在本例中为“,#808000”,然后依此类推每个逗号以 10 个字符为增量。
我尝试结合一些想法,例如在每个 base 6 字符串之间添加 1 个字符来算作 space,但我无法弄清楚。我还开始着手使用一个设置为整数 0 的变量,在每个字符串转换回十六进制后该变量将递增 10。
但是我还没有从中得出任何结果,所以我希望这里的人可以阻止我在我可以使用更简单的解决方案时走得太远。
while str(cyinput2) != "":
S = 0
S1 = 9
hexstart = ','
hexend = ':'
start_index = cyinput2.find(hexstart) + len(hexstart)
end_index = cyinput2.find(hexend)
block = cyinput2[start_index:end_index]
print(block)
您可以使用拆分将字符串简单地转换为列表,
如果你想要干净的块,你也可以使用替换
# Removing useless chars in order to have a clean split
cyinput2 = cyinput2.replace(',', '')
cyinput2 = cyinput2.replace(':', '')
# From : ",#96c8a2: ,#808000: ,#96c8a2: ,#e1a95f: ,#808000: ,#6f00ff:"
# to : "#96c8a2 #808000 #96c8a2 #e1a95f #808000 #6f00ff"
# Split
cyinput2 = cyinput2.split(' ')
# From : "#96c8a2 #808000 #96c8a2 #e1a95f #808000 #6f00ff"
# to : ["#96c8a2", "#808000", "#96c8a2", "#e1a95f", "#808000", "#6f00ff"]
for block in cyinput2:
print(block)
你可以使用这个 while 循环
cyinput2 = ",#96c8a2: ,#808000: ,#96c8a2: ,#e1a95f: ,#808000: ,#6f00ff: "
while len(str(cyinput2)) > 9 :
print(cyinput2[1:10])
cyinput2 = cyinput2.replace(cyinput2[0:10],'')
Output
#96c8a2:
#808000:
#e1a95f:
#6f00ff:
所以我正在编写自己的密码器和解码器来找点乐子,但我被困在我的解密程序中,我需要在我的字符串中向上移动 1 个字符才能解密下一个“块”字符数。
cyinput2 = ",#96c8a2: ,#808000: ,#96c8a2: ,#e1a95f: ,#808000: ,#6f00ff:"
#989E86
上面是我尝试向上移动的字符串,本质上我是在尝试从第一个 6 进制字符串移动到第二个字符串,在本例中为“,#808000”,然后依此类推每个逗号以 10 个字符为增量。
我尝试结合一些想法,例如在每个 base 6 字符串之间添加 1 个字符来算作 space,但我无法弄清楚。我还开始着手使用一个设置为整数 0 的变量,在每个字符串转换回十六进制后该变量将递增 10。 但是我还没有从中得出任何结果,所以我希望这里的人可以阻止我在我可以使用更简单的解决方案时走得太远。
while str(cyinput2) != "":
S = 0
S1 = 9
hexstart = ','
hexend = ':'
start_index = cyinput2.find(hexstart) + len(hexstart)
end_index = cyinput2.find(hexend)
block = cyinput2[start_index:end_index]
print(block)
您可以使用拆分将字符串简单地转换为列表, 如果你想要干净的块,你也可以使用替换
# Removing useless chars in order to have a clean split
cyinput2 = cyinput2.replace(',', '')
cyinput2 = cyinput2.replace(':', '')
# From : ",#96c8a2: ,#808000: ,#96c8a2: ,#e1a95f: ,#808000: ,#6f00ff:"
# to : "#96c8a2 #808000 #96c8a2 #e1a95f #808000 #6f00ff"
# Split
cyinput2 = cyinput2.split(' ')
# From : "#96c8a2 #808000 #96c8a2 #e1a95f #808000 #6f00ff"
# to : ["#96c8a2", "#808000", "#96c8a2", "#e1a95f", "#808000", "#6f00ff"]
for block in cyinput2:
print(block)
你可以使用这个 while 循环
cyinput2 = ",#96c8a2: ,#808000: ,#96c8a2: ,#e1a95f: ,#808000: ,#6f00ff: "
while len(str(cyinput2)) > 9 :
print(cyinput2[1:10])
cyinput2 = cyinput2.replace(cyinput2[0:10],'')
Output
#96c8a2:
#808000:
#e1a95f:
#6f00ff: