保存以空格为分隔符的文件并将其读取回来,可以使用Python的文件处理功能来实现。下面是一个示例代码:
保存文件:
data = "Hello World! This is a test file."
# 写入文件
with open("data.txt", "w") as file:
file.write(data)
# 读取文件
with open("data.txt", "r") as file:
content = file.read()
# 以空格分割数据
words = content.split(" ")
print(words)
读取文件:
# 读取文件
with open("data.txt", "r") as file:
content = file.read()
# 以空格分割数据
words = content.split(" ")
print(words)
在这个示例中,我们首先将要保存的数据写入到名为"data.txt"的文件中。然后,我们使用相同的文件名再次打开文件,并读取文件内容。最后,我们使用split()函数将读取到的内容按照空格进行分割,并将分割后的结果存储在一个列表中。
下一篇:保存依赖两个表的记录