在上一段的空白处输入文本数据

2024-09-29 23:17:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个myplants.txt文件显示在下面:

Caladium White Christmas
2020-03-07 10:10:20
Partial sun to bright full shade
Direct sun might burn Leaves
Water regularly and never allow soil to dry out
Soil must be loose and well draining

Sansevieria Trifasciata (Snake Plant)
2020-05-22 09:56:49
Full shade to full sun
Well-draining soil
Water only when top 1 inch of soil is dry
Propagate by cuttings
1
2
3
chilli
2020-08-21, 03:20:43
hot

如果您注意到,辣椒数据是在前面的Sansevieria trifasicata(蛇形植物)段落的3个空白处输入的。如何确保用户输入的数据与上一段正好相隔一个空格

以下是我的附加代码:

print("Welcome to myplants dictionary database!")


print("Opening the file...")
target = open('myplants.txt', 'a')
# can open the file to w- write, read and write or append
print("Now I'm going to ask you for the details.")

target.write("\n")
line1 = input("What is the new plant name? ")
target.write("\n")
target.write(line1)
target.write("\n")

from datetime import datetime
now = datetime.now()
target.write(now.strftime("%Y-%m-%d, %H:%M:%S"))
date_time = now.strftime("%Y-%m-%d, %H:%M:%S")
print("Latest revision time is: ",date_time)


target.write("\n")
line3 = input("What is the description of the plant? ")
target.write(line3)

print("I'm going to write these to the file.")

target.close()
print("And finally, we close it.")

Tags: andthetotxttargetdatetimetimeis
1条回答
网友
1楼 · 发布于 2024-09-29 23:17:25

要使“chilli”和日期不出现在同一行中,应将line1变量和“\n”字符串连接起来,而不是使用单独的写函数来写新行。这就是为什么它看起来像那样分散的原因

print("Welcome to myplants dictionary database!")


print("Opening the file...")
target = open('myplants.txt', 'a')
# can open the file to w- write, read and write or append
print("Now I'm going to ask you for the details.")

target.write("\n\n")
line1 = input("What is the new plant name? ")
target.write(line1 + '\n')

from datetime import datetime
now = datetime.now()
target.write(now.strftime("%Y-%m-%d, %H:%M:%S"))
date_time = now.strftime("%Y-%m-%d, %H:%M:%S")
print("Latest revision time is: ",date_time)


target.write("\n")
line3 = input("What is the description of the plant? ")
target.write(line3)

print("I'm going to write these to the file.")
target.close()
print("And finally, we close it.")

相关问题 更多 >

    热门问题