Python入门(六)

文件处理

  1. 文件读取
f=open("test.txt","r")       #"rb"以二进制模式读

g=f.read()
print(g)

f.close()  

#文件读取,一定要关闭
readline(),遇到\n or \r为止
2. 文件写

f=open("test_write.txt","w")

f.write('123456')
f.write('\n')
f.write('234567')

f.close()

#文件写操作,是否已创建都行
3. 文件操作

weather_data=[]
f=open("la_weather.csv","r")

data=f.read()
rows=data.split('\n')
print(rows)
for row in rows:
    split_row=row.split(",")
    weather_data.append(split_row)
print(weather_data)

#[‘1,sunny’,’2,sunny’,’3,sunny’,’4,sunny’,’5,sunny’,’6,rain’,’7,sunny’,’8,sunny’,’9,fog’,’10,rain’]
#[[‘1,sunny’],[‘2,sunny’],[‘3,sunny’],[‘4,sunny’],[‘5,sunny’],[‘6,rain’],[‘7,sunny’],[‘8,sunny’],[‘9,fog’],[‘10,rain’]]

weather=[]
for row in weather_data:
    weather.append(row[1])
print(weather)

f.close()

#[‘sunny’,’sunny’,’sunny’,’sunny’,’sunny’,’rain’,’sunny’,’sunny’,’fog’,’rain’]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值