简体   繁体   English

Python 3.x - 将生成的字母作为列表写入文本文件

[英]Python 3.x - writing generated letters as list to a text file

(Code is cutted down bcs its most of the time the same) Im having a Problem: I wrote a program that generates words / numbers based on user wishes and i want them to print in a txt file. (代码在大部分时间内都是相同的)我有一个问题:我写了一个程序,根据用户的意愿生成单词/数字,我希望它们在txt文件中打印。 1 word per line. 每行1个字。 The problem is, If i do this (look below) it works for all numbers/words that are only 2 letters. 问题是,如果我这样做(见下文),它适用于只有2个字母的所有数字/单词。 When I use 3 ore more letters, it looks like this: 当我使用3个以上的字母时,它看起来像这样:

(One per line, dont know how to do this in that post..) AA AA A BA A CA A DA A EA A FA A GA A HA A IA A JA A KA A LA A MA A NA A OA A PA A QA A RA A SA A TA A UA A VA A WA A XA A YA A ZA B AA B BA B CA B (每行一个,不知道如何在那篇文章中做到这一点..)AA AA A BA A CA A DA A EA A FA A GA A HA A IA A JA A KA A LA A MA A NA A OA A PA A QA A RA A SA A TA A UA A VA A WA A XA A YA A ZA B AA B BA B CA B

the output in cmd is right, but txt file is wrong. cmd中的输出是正确的,但txt文件是错误的。 So, did i something wrong? 那么,我有什么不对吗? (ps. dont blame me for that shitty code, not the best ik) (pps hope you unterstand the code bcs its writting in german :/) (ps。不要责怪我那些糟糕的代码,而不是最好的ik)(pps希望你能用德语解读代码bcs的写作:/)

from colorama import Fore
from colorama import init
from os import system
import itertools

AZ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"


init()

def Menu1():
    print(Fore.GREEN + "Dies ist ein Listengenerator. Bitte wähle deine Einstellungen")
    print(Fore.CYAN + "Grossbuchstaben: ", Fore.RED + "1")
    print(Fore.CYAN + "Kleinbuchstaben: ", Fore.RED + "2")
    print(Fore.CYAN + "Gross- & Kleinbuchstaben: ", Fore.RED + "3")
    print(Fore.CYAN + "Zahlen: ", Fore.RED + "4")
    print(Fore.CYAN + "Grossbuchstaben & Zahlen: ", Fore.RED + "5")
    print(Fore.CYAN + "Kleinbuchstaben & Zahlen: ", Fore.RED + "6")
    print(Fore.CYAN + "Gross & Kleinbuchstaben sowie Zahlen: ", Fore.RED + "7")
    print(Fore.CYAN + "")
    user_input = input("")
    global x
    if user_input == "1":
        x=1

Menu1() 

if x==1:
    def x1():
        with open("Grossbuchstaben.txt", "w") as f:
            x = int(input("Minimale Zeichenlänge: "))
            y = int(input("Maximale Zeichenlänge: "))
            for n in range(x, y+1):
                for xs in itertools.product(AZ, repeat=n,):
                    f.write("\n".join(xs))
                    print("".join(xs)) #shows letters in console for seeing when its done
if x==1:
    x1()

If you are saying that the output in the python interpreter is right but the output on the file is wrong, then maybe you should try actually writing the same thing. 如果你说python解释器中的输出是正确的但是文件的输出是错误的,那么也许你应该尝试实际写同样的东西。

In your code you output to the file 在您的代码中输出到文件

"\n".join(xs)

but you output to the interpreter 但是你输出到解释器

"".join(xs)

The first one will concatenate all elements of xs with newlines in between them, while the second one will just concatenate all elements of xs with nothing in between them. 第一个将连接xs所有元素与它们之间的换行符,而第二个将只连接xs所有元素,它们之间没有任何内容。

You probably want to do 你可能想做

f.write("".join(xs) + "\n")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM