Python,将整数写入".txt"文件 [英] Python, writing an integer to a '.txt' file

查看:1197
本文介绍了Python,将整数写入".txt"文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用pickle函数是将整数写入文本文件的最快,最可靠的方法吗?

Would using the pickle function be the fastest and most robust way to write an integer to a text file?

这是我到目前为止的语法:

Here is the syntax I have so far:

import pickle

pickle.dump(obj, file)

如果有更强大的替代方法,请随时告诉我.

If there is a more robust alternative, please feel free to tell me.

我的用例是编写用户输入:

My use case is writing an user input:

n=int(input("Enter a number: "))

  • 是的,一个人将需要阅读并可能对其进行编辑
  • 文件中将有10个数字
  • Python可能需要稍后再读一遍.
  • 推荐答案

    我认为这样做更简单:

    number = 1337
    
    with open('filename.txt', 'w') as f:
      f.write('%d' % number)
    

    但这确实取决于您的用例.

    But it really depends on your use case.

    这篇关于Python,将整数写入".txt"文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆