首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法知道将我的结果导出到文本文件中有什么问题

无法知道将我的结果导出到文本文件中有什么问题
EN

Stack Overflow用户
提问于 2022-06-15 01:34:32
回答 1查看 33关注 0票数 0

这段代码的目标是在回答了几个命令提示问题之后,生成一组随机的密码,将这些密码保存到文本文档中,供以后查看/编辑。我遇到了一个文本文档实际上没有被编辑的问题,在阅读了一些类似的问题之后,我做了一些调整。不过我还是需要帮助!

代码语言:javascript
复制
for password in passwords:
with open('SavedPasswords.rtf' , 'a') as f:

#the following loops create the password(s) and print them to console

if special_chars == "YES" :
    for pwd in range(amount):
        passwords = ''
        for c in range(length):
            passwords += random.choice(chars)
        print(passwords)
        f.write(passwords)

elif special_chars == "NO" :
    for pwd in range(amount):
        passwords = ''
        for c in range(length):
            passwords += random.choice(chars2)
        print(passwords)
        f.write(passwords)
EN

回答 1

Stack Overflow用户

发布于 2022-06-15 01:56:58

当我重新构建你的案子时,我就是这么做的:

代码语言:javascript
复制
import random
import string    # For string module

passwords = ["", "", "", ""]
special_chars = "YES"   # Just for testing
amount = 2
length = 10

# Use the string module to get all ascii char

with_punctuation = string.ascii_letters + string.punctuation
without_punctuation = string.ascii_letters

for password in passwords:
    
    with open('SavedPasswords.rtf' , 'a') as f:

        if special_chars == "YES" :
            for pwd in range(amount):
                passwords = ''
                for c in range(length):
                    passwords += random.choice(with_punctuation)
                print(passwords)
                f.write(passwords + "\n")   # Add this to separate them, I think a csv is better!

        elif special_chars == "NO" :
            for pwd in range(amount):
                passwords = ''
                for c in range(length):
                    passwords += random.choice(without_punctuation)
                print(passwords)
                f.write(passwords + "\n")

你没有说出你到底做了什么,所以这是我所能做的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72625016

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档