python - 如何在 Python3 中使用/n?

标签 python newline

为了逐行而不是逐行获取输出,我想开始使用/n 命令。
这是我认为应该放置的代码段:

password_for = input('This password is for: ')
your_pass =  'Your password for {} is: {}'.format(password_for, password)

save_path = '/Users/"MyUsername"/Desktop'
name_of_file = input("What is the name of the file: ")
completeName = os.path.join(save_path, name_of_file+".txt")
with open(completeName, "a+") as file1:
    file1.write(your_pass)
/n 命令应该用于逐行获取应该写入的文本(输出),如下所示:
Input 1
input 2
但现在输出是这样工作的:
Input1Input2
也许/N 不是解决方案?让我知道!

最佳答案

某些字符必须“转义”才能将它们输入到字符串中。在这种情况下,您需要一个换行符,写成 \n在 Python 中。
无论如何,回答你的问题:

with open(completeName, "a+") as file1:
    file1.write(your_pass + '\n')
这将连接 your_pass 中的字符串使用包含一个换行符的字符串,然后调用 file1.write .

关于python - 如何在 Python3 中使用/n?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67959883/

相关文章:

python - pd.read_html 更改了数字格式

Python 多个正则表达式替换

python - 在 Pandas 数据框中对重复的列 ID 进行分组

c# - 只匹配一个换行符

regex - 与 $ 元字符相关的 "string-ending newline"是什么?

python - 标签字段问题 - OpenErp

python - 为什么 Python 允许序列的超出范围的切片索引?

java - 读取文件时忽略新行

检查输入中的换行符

linux - Linux 提示符中的新行字符?