python - 如何使列表接受/n,以便在写入文件时不让所有内容都在同一行?

标签 python string join

所以这是我的随机序列号生成器的代码,我不知道如何使每个序列号出现在不同的行上,因为它们之间只有空格,我如何使它们出现在新线?

#!/usr/bin/python
import random, hashlib, os
from sys import exit
from time import sleep
database_check1 = os.path.expanduser('~/Codemaker/database.txt')
codemaker_dir_check1 = os.path.expanduser('~/Codemaker')
database_check = os.path.isfile(database_check1)
codemaker_dir_check = os.path.isdir(codemaker_dir_check1)

if codemaker_dir_check == True:
    pass
else:
    os.system('mkdir ~/Codemaker')

def choice():

    if database_check == True:
        replace_or_no = raw_input('Warning database.txt already exists   do   you want to relace it? y = yes n=no a = add more to end of file.       y/n/a?:   ')
        if replace_or_no == ('y'):
            os.system('rm ~/Codemaker/database.txt')
            os.system('> ~/Codemaker/database.txt')              

        elif replace_or_no == ('a'):
            pass
        elif replace_or_no == ('n'):
            print('We did not change the database :)')
            exit()

        else:
            print('An error has occured you probably pressed the wrong         button     if you wish to try again it should work')
            exit()
    else:
        os.system('> ~/Codemaker/database.txt')

Acceptable_letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
gencodelist = []
choice()
database_file_append1 = os.path.expanduser('~/Codemaker/database.txt')
database_file_append = open(database_file_append1, 'a')



def gencode():
    for i in ('hih'):
        for i in ('hihi'):
            gencodelist.append(random.choice(Acceptable_letters))

        gencodelist.append('-')
    for i in ('hihi'):
        gencodelist.append(random.choice(Acceptable_letters))

    gencodelist.append(' ')

for x in xrange(0, 100):
    gencode()
    gencodeout = ''.join(gencodelist)
    print(gencodeout)
    database_file_append.write(gencodeout)
message = ['100 codes have generated and been written to database.txt  located  in ', database_file_append1, '! :)']
finalmessage = "".join(message)
print(finalmessage)

最佳答案

替换

gencodelist.append(' ')

gencodelist.append('\n')

此代码更改后它可以正常工作。

关于python - 如何使列表接受/n,以便在写入文件时不让所有内容都在同一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37717563/

相关文章:

python - 如何在 Airflow 中设置 emr_default

python - 如何在 Python 中合并列表中的两个元素

python - 有没有办法从 Python 的日期时间获取 Django 的默认日期和时间字符串?

sql - 如何使用两个表执行更新

MySQL JOIN 两个表,每个表只有一行和一个属性

python - Python中如何进行封装?

mysql - ColdFusion 仅返回数据库中的部分字符串

string - 按含义比较两个字符串

PHP Spintax 处理器

php - MYSQL:计算另一个表中与第一个表中的行相对应的行数