先从百度上下载一个百万位数的圆周率文件,打开:
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位数传入变量中供你使用了