【问题标题】:Key-Generator in a .txt file.txt 文件中的密钥生成器
【发布时间】:2021-05-15 13:22:01
【问题描述】:

我给你一个例子我点击运行在控制台中写一个数字(多少个键)然后我得到这个随机字母和数字:61PYY-ZEPY1-2H82R-V1JZ1-9VEF7,但我希望它们在我的 demofile2.txt。当我将其粘贴到写入中时:'-'.join(''.join(random.choice(seq) for _ in range(5)) for _ in range(5)) 它说 seq 未定义

这是代码:

import random, sys

class KeyGen():
    def __init__(self):
        global i
        i = int(input("How many serial codes are you looking for? \n"))
        print("")
        self.main(i)

    def main(self, count):
        """ Our main iteration function, using simple
        capital letters, along with the numbers 0-9 """
        seq = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

        for i in range(count):
            print('-'.join(''.join(random.choice(seq) for _ in range(5)) for _ in range(5)))

        print("\nCreated {} serial keys.".format(count))

if __name__ == '__main__':
    app = KeyGen()

text_file = open("demofile2.txt.")
text_file.write(THISISWHATISEARCH)
text_file.close()

【问题讨论】:

  • 这能回答你的问题吗? Print string to text file
  • 我知道如何打开文件并写入文件,但不知道密钥代码是什么
  • 您的问题是:“我不知道如何通过 python 写入 .txt 文件”,现在您说您知道。你的问题对我来说不是很清楚。我没有看到任何尝试打开文件或在代码中写入文件
  • text_file = open("demofile2.txt.") text_file.write(这部分是我在这里搜索的应该是key) text_file.close()
  • print 替换为text_file.write....?

标签: python file


【解决方案1】:

要写入文件,您只需将字符串传递给file.write()。在您的情况下,您正在打印相同的字符串。

一个区别是print 自动在每个字符串的末尾添加一个新行(因为参数end='\n' 的默认值)。因此,在写入文件时,您可以添加一个结束换行符。或者,您可以使用printfile 参数来避免任何更改!

现在你只需要在你想写入的地方有可用的文件描述符。这可能取决于其余代码的设计,为简单起见,我将简单地将其移至 main 函数。

还有一点是在读/写文件时,最好使用withcontext manager

import random, sys

class KeyGen():
    def __init__(self):
        global i
        i = int(input("How many serial codes are you looking for? \n"))
        print("")
        self.main(i)

    def main(self, count):
        """ Our main iteration function, using simple
        capital letters, along with the numbers 0-9 """
        seq = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

        with open("demofile2.txt.", 'w') as text_file:
            for i in range(count):
                print('-'.join(''.join(random.choice(seq) for _ in range(5)) for _ in range(5)), file=text_file)
                # text_file.write('-'.join(''.join(random.choice(seq) for _ in range(5)) for _ in range(5)) + '\n')

            print("\nCreated {} serial keys.".format(count), file=text_file)
            # text_file.write("\nCreated {} serial keys.\n".format(count))

if __name__ == '__main__':
    app = KeyGen()

我将利用这个机会提供一般提示:

在 Python 中没有必要过度使用类。也许你来自 Java 背景,一切都是类,但在这里你的类只是作为一个函数的网关。所以你的代码可能是:

import random, sys

def main():
    """ 
    Our main iteration function, using simple
    capital letters, along with the numbers 0-9 
    """
    seq = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

    count = int(input("How many serial codes are you looking for? \n"))
    with open("demofile2.txt.", 'w') as text_file:
        for _ in range(count):
            print('-'.join(''.join(random.choice(seq) for _ in range(5)) for _ in range(5)), file=text_file)
            # text_file.write('-'.join(''.join(random.choice(seq) for _ in range(5)) for _ in range(5)) + '\n')

        print("\nCreated {} serial keys.".format(count), file=text_file)
        # text_file.write("\nCreated {} serial keys.\n".format(count))

if __name__ == '__main__':
    main()

【讨论】:

  • 我收到此错误: print(file=text_file, '-'.join(''.join(random.choice(seq) for _ in range(5)) for _ in range(5) ))) ^ SyntaxError: 位置参数跟随关键字参数
  • 新错误:第 20 行,在 main() 类型错误:main() 缺少 2 个必需的位置参数:'self' 和 'count'
  • 我刚开始学习编码,对 pycharm 和字面意思都不太了解,所以没有
  • 这是我写的第一个代码
  • 所以你不应该跳到你可以处理的更高级的东西上。循序渐进。首先按顺序查看The Python Tutorial。然后找一些编码挑战的网站(有很多),开始做一些难度增加的挑战
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-18
  • 2010-11-29
  • 1970-01-01
  • 2011-02-22
  • 2017-08-04
  • 2017-05-11
相关资源
最近更新 更多