Python读取csv文件

2019-12-28

创建一个csv文件,命名为data.csv,文本内容如下:

root,123456,login successfully
root,wrong,wrong password
wrong,123456,nonexistent username
,123456,username is null
root,,password is null

使用Excel打开如图:

Python读取csv文件

利用Python内置的csv读取内容:

import csv

with open('data.csv', 'r', encoding='utf-8')as f:

    reader = csv.reader(f)

    for i in reader:
        print(i)

输出结果:

['root', '123456', 'login successfully']
['root', 'wrong', 'wrong password']
['wrong', '123456', 'nonexistent username']
['', '123456', 'username is null']
['root', '', 'password is null']

相关推荐