2024-06-02 05:09:35 发布
网友
使用pickle函数是将整数写入文本文件的最快和最健壮的方法吗?
以下是我目前掌握的语法:
import pickle pickle.dump(obj, file)
如果有更好的选择,请随时告诉我。
我的用例正在编写用户输入:
n=int(input("Enter a number: "))
我觉得这样做比较简单:
number = 1337 with open('filename.txt', 'w') as f: f.write('%d' % number)
但这实际上取决于您的用例。
result = 1 f = open('output1.txt','w') # w : writing mode / r : reading mode / a : appending mode f.write('{}'.format(result)) f.close()
f = open('output1.txt', 'r') input1 = f.readline() f.close() print(input1)
使用python 2,您还可以执行以下操作:
number = 1337 with open('filename.txt', 'w') as f: print >>f, number
我个人在不需要格式化的时候使用这个。
我觉得这样做比较简单:
但这实际上取决于您的用例。
写
阅读
使用python 2,您还可以执行以下操作:
我个人在不需要格式化的时候使用这个。
相关问题 更多 >
编程相关推荐