Request模拟登陆

csv的读取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#demo1
import csv
with open('xxx.csv','r') as f:
reader = csv.reader(f)
#reader是一个迭代器
#可以使用next(reader)跳过第一行的标题
for x in reader:
print(x)

#demo2
import csv
with open('xxx.csv','r') as f:
reader = csv.DictReader(f)
for x in reader:
#使用DictReader方法是不打印title的
#可以通过取键名获取值
print(x['turnoverVol'])

csv的写入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# demo1
import csv
headers = ['name','age','classroom']
values = [
('zhiliao',18,'111'),
('wena',20,'222'),
('bbc',21,'111')
]
with open('xxx.csv','w',encoding='utf-8',newline='') as f:
writer = csv.writer(fp)
writer.writerow(headers)
writer.writerows(values)

# demo2
import csv
headers = ['name','age','classroom']
values = [
{"name":'wenn',"age":20,"classroom":'222'},
{"name":'abc',"age":30,"classroom":'333'}
]
with open('xxx.csv','w',encoding='utf-8',newline='') as f:
writer = csv.DictWriter(fp,headers)
writer = csv.writeheader()
writer.writerow({'name':'zhiliao',"age":18,"classroom":'111'})
writer.writerows(values)
煌金 wechat
扫描关注公众号,回复「1024」获取为你准备的特别推送~
  • 本文作者: 煌金 | 微信公众号【咸鱼学Python】
  • 本文链接: http://www.xianyucoder.cn/2018/07/29/about_csv_read_and_write/
  • 版权声明: 本博客所有文章除特别声明外,均采用 许可协议。转载请注明出处!
  • 并保留本声明和上方二维码。感谢您的阅读和支持!