Python 将文本打印到文本文件中的程序

Python 将文本打印到文本文件中的程序,python,Python,我需要创建一个程序,接收用户的身份证号码,创建一个学校注册系统,然后将信息导出到一个.txt文件中。我的问题是我没能把输入的每个身份证号码都写在新行上,我需要帮助 我将每个ID放入一个列表中,然后将它们全部合并成一个字符串。现在我需要在单独的一行上打印字符串中的每个ID号。请告诉我在何处插入“\n”,以便它在txt文件的新行上打印 # we first define the name of the file and set it to write mode to_file = open("Reg

我需要创建一个程序,接收用户的身份证号码,创建一个学校注册系统,然后将信息导出到一个.txt文件中。我的问题是我没能把输入的每个身份证号码都写在新行上,我需要帮助

我将每个ID放入一个列表中,然后将它们全部合并成一个字符串。现在我需要在单独的一行上打印字符串中的每个ID号。请告诉我在何处插入“\n”,以便它在txt文件的新行上打印

# we first define the name of the file and set it to write mode
to_file = open("RegForm.txt" , "w")

# list variable for storing the received i.d. numbers
id_numbers = []

# asking the user to enter the number of students that will write the exam
num = int(input("Please enter the number of students that will sit for the exam: "))



# creating a loop that iterates over every student who is writing the exam
# we then append the list of id numbers with each new input we receive

for toFile in range(0, num):
    id_numbers.append(input("Enter your ID Number: " ))

# we create the variable string_of_nums which is joining the list into a single string
string_of_nums = " ".join(id_numbers)

# writing the id numbers onto the text file
to_file.write(string_of_nums)

# closing the file
to_file.close()

我需要它在单独的一行上打印每个id号

string\u/u nums=“\n”。join(id\u number)
string\u/u nums=“\n”。join(id\u numbers)
如果要在每行末尾添加换行符(
'\n'
),请在将行连接在一起时使用换行符作为连接字符串

string_of_nums = "\n".join(id_numbers)
string_of_nums = "\n".join(id_numbers)

要在每行末尾添加换行符(
'\n'
),请在将行连接在一起时使用换行符作为连接字符串

string_of_nums = "\n".join(id_numbers)
string_of_nums = "\n".join(id_numbers)

您当前的字符串用空格分隔每个学生ID

string_of_nums = " ".join(id_numbers)
join()定义和用法

join()方法获取一个iterable中的所有项,并将它们连接成一个字符串。 必须将字符串指定为分隔符

因此,“”中的任何内容都将成为字符串分隔符

因此,要使用换行符分隔每个学生,我们必须插入\n:


您当前的字符串用空格分隔每个学生ID

string_of_nums = " ".join(id_numbers)
join()定义和用法

join()方法获取一个iterable中的所有项,并将它们连接成一个字符串。 必须将字符串指定为分隔符

因此,“”中的任何内容都将成为字符串分隔符

因此,要使用换行符分隔每个学生,我们必须插入\n: