我试图通过改变一个打开的文件来编写一个程序,我需要在打印中添加一行新行。在
公开的txt文件,如下所示(我使用“”替换空白):
姓名身高(m)体重(kg)
1.58比尔
Mary_优逯uu轱u1.65轱uuuuu43
。。。在
现在我想添加一个新的行,如下所示: 名称-高度(m)、重量(kg)、年龄(年)<;——新的垂直线
清单15
玛莉廑
我的代码是:
data_file = open("file.txt", "r")
print(data_file.read())
data_file.close()
我怎么能在另一个垂直的行中打开文件?此外,如果我想添加更多的行,我该怎么做呢?在
还有一件事,我使用python3.5
如果“addanewverticalline”的意思是“addanewolumn”,那么可以借助^{} 模块来实现这一点。在
以下代码的工作原理是将文件的内容作为列表读取,进行更改,然后将更新后的列表写回文件中。也可以用这种方法向文件中添加行。在
在处理文件时,此代码还使用} 或^{} (就像你在问题中提到的那样)是正确的做法。
with
语句。在处理文件时,使用^{with
语句很容易使用,因为它会自动关闭文件。在我写了一个小类来完成你要求的一切。实现示例在底部完成。告诉我这对你有用吗。在
相关问题 更多 >
编程相关推荐