Python Read然后写项目

时间:2017-02-11 02:56:34

标签: python-2.7 encryption encoding decoding read-write

我正在尝试编写一个程序,该程序将读取文本文件并将其读取的内容转换为另一个文本文件,但使用给定的变量。有点像自制加密。我希望程序一次读取2个字节并读取整个文件。我是python的新手但享受应用程序。任何帮助将不胜感激

a = 12
b = 34
c = 56
等...最多20种不同类型的变量

file2 = open(" textfile2.text"," w")
file = open(" testfile.txt"," r")
file.read(2):
    if file.read(2)= 12 then;
    file2.write("")
      否则如果file.read(2)= 34
    file2.write(" B&#34)
      否则如果file.read(2)= 56
    file2.write(" C")
    file.close()
    file2.close()


文本文件看起来像:
1234567890182555

因此该程序将读取12并写入" a"在另一个文本文件中,然后阅读34并将" b"在另一个文本文件中。只是有一些逻辑问题。

2 个答案:

答案 0 :(得分:0)

我喜欢你的想法是我会怎么做的。注意我使用lower()将所有内容转换为小写但是如果你理解我在做什么,那么将它扩展为小写和大写都很简单:

import string

d = dict.fromkeys(string.ascii_lowercase, 0) # Create a dictionary of all the letters in the alphabet

updates = 0
while updates < 20: # Can only encode 20 characters
    letter = input("Enter a letter you want to encode or type encode to start encoding the file: ")
    if letter.lower() == "encode": # Check if the user inputed encode
        break
    if len(letter) == 1 and letter.isalpha(): # Check the users input was only 1 character long and in the alphabet
        encode = input("What do want to encode %s to: " % letter.lower()) # Ask the user what they want to encode that letter to
        d[letter.lower()] = encode
        updates += 1
    else:
        print("Please enter a letter...")

with open("data.txt") as f:
    content = list(f.read().lower())

for idx, val in enumerate(content):
    if val.isalpha():
        content[idx] = d[val]

with open("data.txt", 'w') as f:
    f.write(''.join(map(str, content)))

print("The file has been encoded!")

使用示例:

原始data.txt:

The quick brown fox jumps over the lazy dog

运行脚本:

Enter a letter you want to encode or type encode to start encoding the file: T                                                                                                                                                                                          
What do want to encode t to: 6                                                                                                                                                                                                                                          
Enter a letter you want to encode or type encode to start encoding the file: H                                                                                                                                                                                          
What do want to encode h to: 8                                                                                                                                                                                                                                          
Enter a letter you want to encode or type encode to start encoding the file: u                                                                                                                                                                                          
What do want to encode u to: 92                                                                                                                                                                                                                                         
Enter a letter you want to encode or type encode to start encoding the file: 34                                                                                                                                                                                         
Please enter a letter...                                                                                                                                                                                                                                                
Enter a letter you want to encode or type encode to start encoding the file: rt                                                                                                                                                                                         
Please enter a letter...                                                                                                                                                                                                                                                
Enter a letter you want to encode or type encode to start encoding the file: q                                                                                                                                                                                          
What do want to encode q to: 9                                                                                                                                                                                                                                          
Enter a letter you want to encode or type encode to start encoding the file: encode                                                                                                                                                                                     
The file has been encoded!

编码data.txt:

680 992000 00000 000 092000 0000 680 0000 000

答案 1 :(得分:0)

我会读取源文件并在转换为字符串时转换项目。然后将整个结果字符串分别写入第二个文件。这也允许您使用更好的with open构造进行文件读取。这允许python为你处理文件关闭。

此代码无效,因为它只读取前两个字符。你需要创建自己的想法如何迭代它,但这是一个想法(不只是为你做一个解决方案)

with open("textfile.text","r") as f:
    # you need to create a way to iterate over these two byte/char increments
    code = f.read(2)
        decoded = <figure out what code translates to>
        results += decoded

# now you have a decoded string inside `results`

with open("testfile.txt","w") as f:
    f.write(results)

decoded = <figure out what code translates to>部分比使用一堆串行if / elseifs要好得多....

或许定义一个编码字典?

codings = {
    "12": "a",
    "45": "b",
    #  etc...
    }

然后你可以:

results += codings[code]

而不是if语句(而且会更快)。