如何使用索引交换文本文件中的字母,使用两个不同的列表?
How do I use indexing to swap letters from a text file, using two different lists?
我正在编写 encrypting/decrypting 代码,这是我目前所拥有的...
def encrypt():
while True:
try:
userinp = input("Please enter the name of a file: ")
file = open(f"{userinp}.txt", "r")
break
except:
print("That File Does Not Exist!")
second = open("encoded.txt", "w")
for line in file:
line = line.strip()
swap = swapped(line)
print(swap)
change_char = change_character(swap)
reversed = reverse_word(change_char)
second.write(reversed)
file.close()
second.close()
def swapped(line):
line = line.strip()
new = ''
arranged = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
random = ["q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"]
for character in line:
i = arranged.index(character)
if arranged[i] == random[i]:
new = new + random[i]
def change_character(swap):
empty = []
secondary = ""
num = ord(swap)
empty.append(num)
for i in range(len(empty)):
if num == 32:
num = 9888
else:
num = num + 80
secondary = secondary + chr(num)
return secondary
def reverse_word(change_char):
data_1 = change_char[::-1]
return data_1
def decrypt():
file = open(f"encoded.txt", "r")
decode = open("decrypted.txt", "w")
for line in file:
un = unswap(line)
change = unchange_char(un)
normal = unreverse(change)
decode.write(normal)
file.close()
decode.close()
我实际上是在尝试以 3 种方式修改文本文件,将其粘贴到另一个文件,然后使用该文件解密文本文件,并将其放入另一个文件中。
我在这样做时遇到的唯一实际问题是使我的一个更改功能起作用,这是在称为“交换”的加密功能之后的第一个功能。我想要做的是在文本文件中搜索适合排列列表的单词,然后将这些字母与同一索引处的随机列表中的字母交换,以便文本文件相应地更改以更改内容。我已经尝试了很多不同的东西,而我现在正在尝试的东西,我得到了错误...
""不在列表中,
即使只是打印出 arranged[i]。我基本上已经尝试了我能想到的一切,但没有运气。如果可能的话,我希望该函数继续使用我创建的两个列表。谢谢!
出现该错误的原因是 space 字符不在列表中,因此您有两个选择:
- 您可以将 space 添加到两个列表中。
- 您可以查看您当前所在的角色是否是space。
这将解决该问题,但我不确定该函数中的其余逻辑是否正确,此时它只会交换字符,如果它们是完全相同的插槽中的相同字符,这确实不会发生,这意味着您将始终得到一个空字符串。
这是我会做的,不会对你的功能做太多改变:
def swapped(line):
line = line.strip()
new = ''
arranged = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
random = ["a","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"]
for character in line:
if character == ' ':
new += ' '
else:
i = arranged.index(character)
new = new + random[i]
print(new)
swapped("lol this is just a test")
我正在编写 encrypting/decrypting 代码,这是我目前所拥有的...
def encrypt():
while True:
try:
userinp = input("Please enter the name of a file: ")
file = open(f"{userinp}.txt", "r")
break
except:
print("That File Does Not Exist!")
second = open("encoded.txt", "w")
for line in file:
line = line.strip()
swap = swapped(line)
print(swap)
change_char = change_character(swap)
reversed = reverse_word(change_char)
second.write(reversed)
file.close()
second.close()
def swapped(line):
line = line.strip()
new = ''
arranged = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
random = ["q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"]
for character in line:
i = arranged.index(character)
if arranged[i] == random[i]:
new = new + random[i]
def change_character(swap):
empty = []
secondary = ""
num = ord(swap)
empty.append(num)
for i in range(len(empty)):
if num == 32:
num = 9888
else:
num = num + 80
secondary = secondary + chr(num)
return secondary
def reverse_word(change_char):
data_1 = change_char[::-1]
return data_1
def decrypt():
file = open(f"encoded.txt", "r")
decode = open("decrypted.txt", "w")
for line in file:
un = unswap(line)
change = unchange_char(un)
normal = unreverse(change)
decode.write(normal)
file.close()
decode.close()
我实际上是在尝试以 3 种方式修改文本文件,将其粘贴到另一个文件,然后使用该文件解密文本文件,并将其放入另一个文件中。
我在这样做时遇到的唯一实际问题是使我的一个更改功能起作用,这是在称为“交换”的加密功能之后的第一个功能。我想要做的是在文本文件中搜索适合排列列表的单词,然后将这些字母与同一索引处的随机列表中的字母交换,以便文本文件相应地更改以更改内容。我已经尝试了很多不同的东西,而我现在正在尝试的东西,我得到了错误...
""不在列表中,
即使只是打印出 arranged[i]。我基本上已经尝试了我能想到的一切,但没有运气。如果可能的话,我希望该函数继续使用我创建的两个列表。谢谢!
出现该错误的原因是 space 字符不在列表中,因此您有两个选择:
- 您可以将 space 添加到两个列表中。
- 您可以查看您当前所在的角色是否是space。
这将解决该问题,但我不确定该函数中的其余逻辑是否正确,此时它只会交换字符,如果它们是完全相同的插槽中的相同字符,这确实不会发生,这意味着您将始终得到一个空字符串。
这是我会做的,不会对你的功能做太多改变:
def swapped(line):
line = line.strip()
new = ''
arranged = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
random = ["a","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"]
for character in line:
if character == ' ':
new += ' '
else:
i = arranged.index(character)
new = new + random[i]
print(new)
swapped("lol this is just a test")