python 读取csv文件

1、csv文件读写

import csv

headers = ['class','name','sex','height','year']

rows = [
        [1,'xiaoming','male',168,23],
        [1,'xiaohong','female',162,22],
        [2,'xiaozhang','female',163,21],
        [2,'xiaoli','male',158,21]
    ]

# 写入csv 文件
with open('test.csv','w')as f:
    f_csv = csv.writer(f)
    f_csv.writerow(headers)
    f_csv.writerows(rows)

# 读取csv 文件
with open('/root/test.csv')as f:
    f_csv = csv.reader(f)
    for row in f_csv:
        print(row)

# 输出结果:
['class', 'name', 'sex', 'height', 'year']
['1', 'xiaoming', 'male', '168', '23']
['1', 'xiaohong', 'female', '162', '22']
['2', 'xiaozhang', 'female', '163', '21']
['2', 'xiaoli', 'male', '158', '21']

2、写入字典序列的数据

import csv

headers = ['class','name','sex','height','year']

rows = [
        {'class':1,'name':'xiaoming','sex':'male','height':168,'year':23},
        {'class':1,'name':'xiaohong','sex':'female','height':162,'year':22},
        {'class':2,'name':'xiaozhang','sex':'female','height':163,'year':21},
        {'class':2,'name':'xiaoli','sex':'male','height':158,'year':21},
    ]

# 写入csv 文件
with open('test2.csv','w',newline='')as f:
    f_csv = csv.DictWriter(f,headers)
    f_csv.writeheader()
    f_csv.writerows(rows)

# 读取csv 文件
with open('/root/test2.csv')as f:
    f_csv = csv.reader(f)
    for row in f_csv:
        print(row)

# 输出结果:
['class', 'name', 'sex', 'height', 'year']
['1', 'xiaoming', 'male', '168', '23']
['1', 'xiaohong', 'female', '162', '22']
['2', 'xiaozhang', 'female', '163', '21']
['2', 'xiaoli', 'male', '158', '21']

3、获取csv的每一行

[root@Cloud-Server ~]# more /root/testcsv.csv
,one,two,three
a,1,2,3
b,4,5,6
c,7,9,9
[root@Cloud-Server ~]#

import csv
with open('/root/testcsv.csv')as f:
    f_csv = csv.reader(f)
    for row in f_csv:
        print(row)

# 输出结果:
['', 'one', 'two', 'three']
['a', '1', '2', '3']
['b', '4', '5', '6']
['c', '7', '9', '9']

4、获取csv的第一列

import csv

with open('/root/testcsv.csv', 'r') as f:
    reader = csv.reader(f)
    for row in reader:
        #print(row)
        print(row[0])

# 输出结果:
a
b
c

5、获取csv的第3行

[root@Cloud-Server ~]# more /root/testcsv.csv
,one,two,three
a,1,2,3
b,4,5,6
c,7,9,9
[root@Cloud-Server ~]#

方式一:

import csv

with open('/root/testcsv.csv', 'r') as f:
    reader = csv.reader(f)
    for i,row in enumerate(reader):
        if i == 2:
            res=row
            print(res)

# 输出结果:
['b', '4', '5', '6']

方式二:
使用csv.DictReader(),该函数和csv.reader() 类似,接受一个可迭代的对象,能返回一个生成器,但是返回的没一个单元格都放在一个字典的值内,而这个字段的键则是这个单元格的标题(即列头)

# 读取所有数据
# 1、第一种
import csv

with open('/root/testcsv.csv', 'r') as f:
    reader = csv.DictReader(f)
    rows = [row for row in reader]
    print(rows)

# 输出结果:
[OrderedDict([('', 'a'), ('one', '1'), ('two', '2'), ('three', '3')]), OrderedDict([('', 'b'), ('one', '4'), ('two', '5'), ('three', '6')]), OrderedDict([('', 'c'), ('one', '7'), ('two', '9'), ('three', '9')])]

# 2、第二种
import csv

with open('/root/testcsv.csv', 'r') as f:
    reader = csv.DictReader(f)
    for row in reader:
        print(row)

# 输出结果:
OrderedDict([('', 'a'), ('one', '1'), ('two', '2'), ('three', '3')])
OrderedDict([('', 'b'), ('one', '4'), ('two', '5'), ('three', '6')])
OrderedDict([('', 'c'), ('one', '7'), ('two', '9'), ('three', '9')])


# 3、第三种
import csv

with open('/root/testcsv.csv', 'r') as f:
    reader = csv.DictReader(f)
    for row in reader:
        #print(row)
        print(list(row['one']),list(row['two']),list(row['three']))

# 输出结果:
['1'] ['2'] ['3']
['4'] ['5'] ['6']
['7'] ['9'] ['9']


import csv

with open('/root/testcsv.csv', 'r') as f:
    reader = csv.DictReader(f)
    for row in reader:
        #print(row)
        res=row['one']+row['two']+row['three']
        print(list(res))

# 输出结果:
['1', '2', '3']
['4', '5', '6']
['7', '9', '9']

6、读取指定列

6.1、读取列名为one的数据

# 读取列名为one的数据
import csv

with open('/root/testcsv.csv', 'r') as f:
    reader = csv.DictReader(f)
    for row in reader:
        #print(row)
        print(list(row['one']))

# 输出结果:
['1']
['4']
['7']

6.2、读取列名为two的数据

# 读取列名为two的数据
import csv

with open('/root/testcsv.csv', 'r') as f:
    reader = csv.DictReader(f)
    for row in reader:
        #print(row)
        print(list(row['two']))

# 输出结果:
['2']
['5']
['9']

6.3、读取列名为three的数据

# 读取列名为three的数据
import csv

with open('/root/testcsv.csv', 'r') as f:
    reader = csv.DictReader(f)
    for row in reader:
        #print(row)
        print(list(row['three']))

# 输出结果:
['3']
['6']
['9']

6.4、同时读取列名为one和two的数据

# 同时读取列名为one和two的数据

import csv

with open("/root/testcsv.csv") as csvfile:
    reader = csv.DictReader(csvfile,delimiter=" ")
    print(list(reader))

# 输出结果:
[OrderedDict([(',one,two,three', 'a,1,2,3')]), OrderedDict([(',one,two,three', 'b,4,5,6')]), OrderedDict([(',one,two,three', 'c,7,9,9')])]

import csv

with open('/root/testcsv.csv', 'r') as f:
    reader = csv.DictReader(f)
    for row in map(dict,reader):
        #print(row)
        print(f"{row['one']} {row['two']}")

# 输出结果
1 2
4 5
7 9

7、读取满足条件的行

方式一

import csv

with open('/root/testcsv.csv', 'r') as f:
    reader = csv.DictReader(f)
    for row in reader:
        if row['one']=='1':
            print(list(row))

# 输出结果:
['', 'one', 'two', 'three']

方式二

import csv

with open('/root/testcsv.csv', 'r') as f:
    reader = csv.DictReader(f)
    for row in reader:
        if row['one']=='1':
            print(row)

# 输出结果:
OrderedDict([('', 'a'), ('one', '1'), ('two', '2'), ('three', '3')])

你可能感兴趣的:(python,python)