1、print()输出
a\%输出
a=1
b=1
c=2
print("%d+%d=%d"%(a,b,c))
name="小明"
age=13
print("%s今年%d岁"%(name,age))
pi=3.141592653
print("pi保留小数点后三位为%4.3f"%pi)#字段宽4,精度3
print("pi保留小数点后三位为 %.*f" % (3,pi)) #用*从后面的元组中读取字段宽度或精度
for i in range(0,3):
print(i, end = '' )
b\format实现输出
print("{} {}".format("hello", "world") )
print("{0} {1}".format("hello", "world") ) # 设置指定位置
print("{1} {0} {1}".format("hello", "world") )# 设置指定位置
print("{:.3f}".format(3.1415926))
print("{:+.3f}".format(3.1415926))#带符号保留两位小数
print("{:.0f}".format(3.1415926))#不保留小数
print("{:.2%}".format(0.25))
print("{:.2e}".format(100000000))
print("{:,}".format(100000))
测试结果:
3.142
+3.142
3
25.00%
1.00e+08
100,000
2sys argv 实现文件存储(存储位置加上>)
import sys
import random
n = int(sys.argv[1])
for i in range(0, n):
print(random.randint(0,100))
print(sys.argv[0])
print(sys.argv[1])
3、文件读入
filename = r'C:\Users\asus\Desktop\Python_Test\data2.txt'
file = open(filename, 'a')#追加写入
file.write('1,2,3,4\n5,6,7,8')
file.close()
更多细节参考python的输入
4、pandas实现
filename = r'C:\Users\asus\Desktop\Python_Test\data3.xls' #绝对路径
df = pd.read_excel(filename,header = None)
df.iloc[0,0] = 100
print(df.iloc[0,0])
df.to_csv(r'.\data.csv')#相对路径
补充:
1、tab键查找要存储的文件格式
2、iloc选取文件读取范围
3、(r'.\data.csv')==('./data.csv')
参考博客:https://blog.csdn.net/sinat_29957455/article/details/79059436
4.numpy文件存储
参考博客:https://blog.csdn.net/runner668/article/details/80360828