处理百万位圆周率的txt,用做自己的PI模块

先从百度上下载一个百万位数的圆周率文件,打开:



查看文本内容:
filename = 'txt\pi_digits.txt'
with open(filename) as file_object:
    lines = file_object.readlines()
    
print(lines[0:5])


文本中有换行符和空格,开头还有非数值“PI=”,对文本进行去换行和空格:

filename = 'txt\pi_digits.txt'
with open(filename) as file_object:
    contents = file_object.read()
    
pi = contents.replace(' ','').replace('\n','')[3:]
print(pi[0:100])

这样就可以将你需要的pi位数传入变量中供你使用了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章