相关疑难解决方法(0)

在Python中编写巨大的字符串

我有一个很长的字符串,几乎有一兆字节长,我需要将其写入文本文件。常规的

file = open("file.txt","w")
file.write(string)
file.close()
Run Code Online (Sandbox Code Playgroud)

可以,但是太慢了,有什么办法可以写得更快吗?

我正在尝试将几百万位数字写入文本文件,该数字约为math.factorial(67867957)

这是分析中显示的内容:

    203 function calls (198 primitive calls) in 0.001 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 re.py:217(compile)
        1    0.000    0.000    0.000    0.000 re.py:273(_compile)
        1    0.000    0.000    0.000    0.000 sre_compile.py:172(_compile_charset)
        1    0.000    0.000    0.000    0.000 sre_compile.py:201(_optimize_charset)
        4    0.000    0.000    0.000    0.000 sre_compile.py:25(_identityfunction)
      3/1    0.000    0.000    0.000    0.000 sre_compile.py:33(_compile)
        1    0.000    0.000    0.000    0.000 sre_compile.py:341(_compile_info)
        2    0.000    0.000 …
Run Code Online (Sandbox Code Playgroud)

python performance file-io python-3.x

3
推荐指数
1
解决办法
3579
查看次数

标签 统计

file-io ×1

performance ×1

python ×1

python-3.x ×1