[英]How to edit each line of a text file and write to a new file
My first text file has lines of text for example John Smith:Year 13:12/12/2000 I want to edit each line for example John Smith:Year 13:12/12/2000:Non Active我的第一个文本文件有几行文本,例如 John Smith:Year 12/12/2000 我想编辑每一行,例如 John Smith:Year 13:12/12/2000:Non Active
myFile = open("Books.txt", "r")
for line in myFile:
print(line)
AddPubDate = ":",input("Enter the publication date for book above")
myFile2 = open("Books2.txt", "a")
NewLine = str(line) + str(AddPubDate)
myFile2.write(NewLine)
print(NewLine)
myFile.close()
myFile2.close()
print("Process complete")A
This should work:这应该有效:
myFile = open("Books.txt", "r")
myFile = myFile.read()
lines = myFile.split("\n")
for txt in lines:
print(txt)
AddPubDate = ":",input("Enter the publication date for book above")
NewLine = str(txt) + str(AddPubDate)
with open("books2.txt", "a") as f:
f.write(NewLine+"\n")
print(NewLine)
myFile.close()
print("Process complete")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.