键盘输入:str=input(“输入的提示词:”)
文件输入:
f=open(“temp/文件的地址.txt”,“r”) str=f.read() //读取整个文件 str=f.readline() //读取一行 f.close()
with操作:
with open("/tmp/filename", “r”) as f: str = f.read() print(str) #关闭打开的文件 f.close()
print格式化输出:
import mat h print(‘常量 PI 的值近似为:%5.3f。’ % math.pi) print(‘it is %5.3f %5.4f’ %(math.pi,math.pi))
写入文件:
#打开一个文件 f = open("/tmp/foo.txt", “w”) num = f.write( “这是我写的第一行。\n第二行内容\n” ) print(num) #关闭打开的文件 f.close()
import csv f=open(“fruit.csv”) reader=csv.reader(f) //读取列 // csv.DictReader读取字典 print(list(reader)) f.close()
file=“fruit.csv” with open file as f: reader=csv.reader(f)
file=“fruit.csv” with open (file,“w”,newline=’ ')as f: writer=csv.writer(f) for row in data: writer.writerow(row)
