python中csv模块和join函数的使用

在看项目的时候恰好又碰到了这两个功能,然后稍微记录一下,关于join函数,是一个经常使用的联合函数,作用就是用自己规定的字符去串联字符串和列表之类的,对于字符串来说,join函数针对的是字符串中的每一个字符元素,但是对于列表来说就是针对列表中的每一个元素,不过在字典中join只会直接串联key而不是value,所以如果要value的话要自己指定一下。

#!/usr/bin/env python
# -*- coding:utf-8 -*-

'sep'.join(sep)  #第一个sep是自己想要使用的分隔符,第二个是自己想要操作的对象。
>>>str(sep1)
>>>"['%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s']"
>>>sep1.__str__()
>>>"['%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s']"
>>>''.join(sep1)
>>>'%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s'
>>>''.join('%s' * 10)
>>>'%s%s%s%s%s%s%s%s%s%s'
>>>','.join('%s' * 10)
>>>'%,s,%,s,%,s,%,s,%,s,%,s,%,s,%,s,%,s,%,s'

关于csv模块,我用的比较多的是利用csv将本地文件导入到数据库当中,所以基本上都是读文件:第一种是使用reader,第二种是使用DictReader

data = csv.reader(open(r'C:\Users\欧阳\Desktop\csv专用\test.csv', encoding='utf-8'))
rows = [row for row in data]
rows
[['\ufeff179', '阿尔布开克', 'city0000179', '103', '2', '', '', '', ''], ['180', '安克雷奇', 'city0000180', '105', '2', '', '', '', ''], ['181', '亚特兰大', 'city0000181', '145', '2', '', '', '', ''], ['182', '比林斯', 'city0000182', '127', '2', '', '', '', ''], ['183', '波士顿南', 'city0000183', '102', '2', '', '', '', ''], ['184', '波士顿-北', 'city0000184', '102', '2', '', '', '', ''], ['185', '芝加哥', 'city0000185', '111', '2', '', '', '', ''], ['186', '丹佛', 'city0000186', '125', '2', '', '', '', ''], ['187', '西雅图', 'city0000187', '134', '2', '', '', '', ''], ['188', '莱克海瓦萨', 'city0000188', '147', '2', '', '', '', ''], ['189', '迈阿密', 'city0000189', '140', '2', '', '', '', ''], ['190', '休斯顿', 'city0000190', '114', '2', '', '', '', ''], ['191', '马纳萨斯', 'city0000191', '129', '2', '', '', '', ''], ['192', '拉斯维加斯', 'city0000192', '129', '2', '', '', '', ''], ['193', '洛杉矶', 'city0000193', '120', '2', '', '', '', ''], ['194', '迈阿密南部', 'city0000194', '140', '2', '', '', '', ''], ['195', '明尼阿波利斯', 'city0000195', '112', '2', '', '', '', ''], ['196', '纽约', 'city0000196', '108', '2', '', '', '', ''], ['197', '奥兰多', 'city0000197', '140', '2', '', '', '', ''], ['198', '费城', 'city0000198', '138', '2', '', '', '', ''], ['199', '凤凰城', 'city0000199', '110', '2', '', '', '', ''], ['200', '波特兰', 'city0000200', '133', '2', '', '', '', ''], ['201', '盐湖城', 'city0000201', '119', '2', '', '', '', ''], ['202', '旧金山', 'city0000202', '120', '2', '', '', '', ''], ['203', '圣地亚哥', 'city0000203', '120', '2', '', '', '', ''], ['204', '坦帕', 'city0000204', '140', '2', '', '', '', ''], ['205', '卡尔加里', 'city0000205', '150', '4', '', '', '', ''], ['206', '哈利法克斯', 'city0000206', '151', '4', '', '', '', ''], ['207', '蒙特利尔', 'city0000207', '152', '4', '', '', '', ''], ['208', '多伦多', 'city0000208', '153', '4', '', '', '', ''], ['209', '温哥华', 'city0000209', '154', '4', '', '', '', ''], ['210', '达拉斯', 'city0000210', '114', '2', '', '', '', ''], ['211', '贝灵汉', 'city0000211', '134', '2', '', '', '', '']]

 

reader = csv.DictReader(open(r'C:\Users\欧阳\Desktop\csv专用\test.csv', encoding='utf-8'))
column = [row for row in reader]
column
[OrderedDict([('\ufeff179', '180'), ('阿尔布开克', '安克雷奇'), ('city0000179', 'city0000180'), ('103', '105'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '181'), ('阿尔布开克', '亚特兰大'), ('city0000179', 'city0000181'), ('103', '145'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '182'), ('阿尔布开克', '比林斯'), ('city0000179', 'city0000182'), ('103', '127'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '183'), ('阿尔布开克', '波士顿南'), ('city0000179', 'city0000183'), ('103', '102'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '184'), ('阿尔布开克', '波士顿-北'), ('city0000179', 'city0000184'), ('103', '102'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '185'), ('阿尔布开克', '芝加哥'), ('city0000179', 'city0000185'), ('103', '111'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '186'), ('阿尔布开克', '丹佛'), ('city0000179', 'city0000186'), ('103', '125'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '187'), ('阿尔布开克', '西雅图'), ('city0000179', 'city0000187'), ('103', '134'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '188'), ('阿尔布开克', '莱克海瓦萨'), ('city0000179', 'city0000188'), ('103', '147'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '189'), ('阿尔布开克', '迈阿密'), ('city0000179', 'city0000189'), ('103', '140'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '190'), ('阿尔布开克', '休斯顿'), ('city0000179', 'city0000190'), ('103', '114'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '191'), ('阿尔布开克', '马纳萨斯'), ('city0000179', 'city0000191'), ('103', '129'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '192'), ('阿尔布开克', '拉斯维加斯'), ('city0000179', 'city0000192'), ('103', '129'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '193'), ('阿尔布开克', '洛杉矶'), ('city0000179', 'city0000193'), ('103', '120'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '194'), ('阿尔布开克', '迈阿密南部'), ('city0000179', 'city0000194'), ('103', '140'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '195'), ('阿尔布开克', '明尼阿波利斯'), ('city0000179', 'city0000195'), ('103', '112'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '196'), ('阿尔布开克', '纽约'), ('city0000179', 'city0000196'), ('103', '108'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '197'), ('阿尔布开克', '奥兰多'), ('city0000179', 'city0000197'), ('103', '140'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '198'), ('阿尔布开克', '费城'), ('city0000179', 'city0000198'), ('103', '138'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '199'), ('阿尔布开克', '凤凰城'), ('city0000179', 'city0000199'), ('103', '110'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '200'), ('阿尔布开克', '波特兰'), ('city0000179', 'city0000200'), ('103', '133'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '201'), ('阿尔布开克', '盐湖城'), ('city0000179', 'city0000201'), ('103', '119'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '202'), ('阿尔布开克', '旧金山'), ('city0000179', 'city0000202'), ('103', '120'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '203'), ('阿尔布开克', '圣地亚哥'), ('city0000179', 'city0000203'), ('103', '120'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '204'), ('阿尔布开克', '坦帕'), ('city0000179', 'city0000204'), ('103', '140'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '205'), ('阿尔布开克', '卡尔加里'), ('city0000179', 'city0000205'), ('103', '150'), ('2', '4'), ('', '')]), OrderedDict([('\ufeff179', '206'), ('阿尔布开克', '哈利法克斯'), ('city0000179', 'city0000206'), ('103', '151'), ('2', '4'), ('', '')]), OrderedDict([('\ufeff179', '207'), ('阿尔布开克', '蒙特利尔'), ('city0000179', 'city0000207'), ('103', '152'), ('2', '4'), ('', '')]), OrderedDict([('\ufeff179', '208'), ('阿尔布开克', '多伦多'), ('city0000179', 'city0000208'), ('103', '153'), ('2', '4'), ('', '')]), OrderedDict([('\ufeff179', '209'), ('阿尔布开克', '温哥华'), ('city0000179', 'city0000209'), ('103', '154'), ('2', '4'), ('', '')]), OrderedDict([('\ufeff179', '210'), ('阿尔布开克', '达拉斯'), ('city0000179', 'city0000210'), ('103', '114'), ('2', '2'), ('', '')]), OrderedDict([('\ufeff179', '211'), ('阿尔布开克', '贝灵汉'), ('city0000179', 'city0000211'), ('103', '134'), ('2', '2'), ('', '')])]

写和读也差不多,三个步骤,显示打开文件,然后调用csv的writer方法,然后把数据写进去就可以了。

datas = [['name', 'age'], ['Bob', 14], ['Tom', 23], ['Jerry', '18']]
with open('test.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    for row in datas:
        writer.writerow(row)
        #还可使用writerrows写入多行
        

 

posted @ 2018-02-12 14:00  zzy0306  阅读(1170)  评论(0编辑  收藏  举报