如何将每个单独的输入放到不同的txt文件行中

时间:2018-08-27 07:42:42

标签: python python-3.x

def write_csv_file(filename):
    with open(filename, "w") as csv_file:
        file_name = csv_file
        write = input("please enter the number of times you would like to enter "
                      "a text into then file onto a different line: ")
        write = int(write)
        for write in range(write):
            x = input("please enter what you would like to add: ")
            file_name.write(x)

    exit()


xxx = input("please enter the name of the file you would like to print into: ")
print(write_csv_file(xxx))

2 个答案:

答案 0 :(得分:1)

只需在write方法中添加一个换行符;

file_name.write(f'{x}\n')

\n部分表示换行/换行。

答案 1 :(得分:1)

def write_csv_file(filename):
    with open(filename, "w") as csv_file:
        file_name = csv_file
        write = input("please enter the number of times you would like to enter "
                      "a text into then file onto a different line: ")
        write = int(write)
        for write in range(write):
            x = input("please enter what you would like to add: ")
            file_name.write(x)
            file_name.write('\n')


    exit()


xxx = input("please enter the name of the file you would like to print into: ")
print(write_csv_file(xxx))

在编写文本后添加file_name.write('\n')

\n表示新行或换行符。