zoukankan      html  css  js  c++  java
  • python 读写csv

    对于大多数的CSV格式的数据读写问题,都可以使用csv库。例如,假设你在一个名叫stock.csv文件中
    
    有一些股票市场数据:
    [root@node01 python]# python t5.py 
    ['AA', '39.48', '6/11/2007', '9:36am', '-0.18', '181800']
    ['AIG', '71.38', '6/11/2007', '9:36am', '-0.15', '195500']
    ['AXP', '62.58', '6/11/2007', '9:36am', '-0.46', '935000']
    ['BA', '98.31', '6/11/2007', '9:36am', '+0.12', '104800']
    ['C', '53.08', '6/11/2007', '9:36am', '-0.25', '360900']
    ['CAT', '78.29', '6/11/2007', '9:36am', '-0.23', '225400']
    [root@node01 python]# vim t5.py
    
    [root@node01 python]# python t5.py 
    ['AA', '39.48', '6/11/2007', '9:36am', '-0.18', '181800']
    <type 'list'>
    ['AIG', '71.38', '6/11/2007', '9:36am', '-0.15', '195500']
    <type 'list'>
    ['AXP', '62.58', '6/11/2007', '9:36am', '-0.46', '935000']
    <type 'list'>
    ['BA', '98.31', '6/11/2007', '9:36am', '+0.12', '104800']
    <type 'list'>
    ['C', '53.08', '6/11/2007', '9:36am', '-0.25', '360900']
    <type 'list'>
    ['CAT', '78.29', '6/11/2007', '9:36am', '-0.23', '225400']
    <type 'list'>
    
    
    
    [root@node01 python]# cat t5.py 
    import csv
    with open('stocks.csv') as f:
       f_csv = csv.reader(f)
       headers = next(f_csv)
       for row in f_csv:
         print row
         print type(row)
    
    
    在上面的代码中,row是一个列表,因此为了访问某个字段,你需要使用下标。
    
    如row[0] 访问Symbol,row[4]访问Change.
    
    由于这种下标访问通常会引起混淆,你可以考虑使用命名元组,例如:
    
    
    另外一个选择就是讲数据读取到一个字典序列中去,可以这样做:
    
    [root@node01 python]# cat t6.py 
    import csv
    with open('stocks.csv') as f:
     f_csv = csv.DictReader(f)
     for row in f_csv:
       print row
       print type(row)
    
    [root@node01 python]# python t6.py 
    {'Symbol': 'AA', 'Volume': '181800', 'Time': '9:36am', 'Date': '6/11/2007', 'Price': '39.48', 'Change': '-0.18'}
    <type 'dict'>
    {'Symbol': 'AIG', 'Volume': '195500', 'Time': '9:36am', 'Date': '6/11/2007', 'Price': '71.38', 'Change': '-0.15'}
    <type 'dict'>
    {'Symbol': 'AXP', 'Volume': '935000', 'Time': '9:36am', 'Date': '6/11/2007', 'Price': '62.58', 'Change': '-0.46'}
    <type 'dict'>
    {'Symbol': 'BA', 'Volume': '104800', 'Time': '9:36am', 'Date': '6/11/2007', 'Price': '98.31', 'Change': '+0.12'}
    <type 'dict'>
    {'Symbol': 'C', 'Volume': '360900', 'Time': '9:36am', 'Date': '6/11/2007', 'Price': '53.08', 'Change': '-0.25'}
    <type 'dict'>
    {'Symbol': 'CAT', 'Volume': '225400', 'Time': '9:36am', 'Date': '6/11/2007', 'Price': '78.29', 'Change': '-0.23'}
    <type 'dict'>
    
    
    
    为了写入CSV数据,你仍然可以使用csv模块,不过这时候创建一个writer对象
    
    [root@node01 python]# cat t7.py 
    import csv
    headers = ['Symbol','Price','Date','Time','Change','Volume']
    rows = [('AA', 39.48, '6/11/2007', '9:36am', -0.18, 181800),
    ('AIG', 71.38, '6/11/2007', '9:36am', -0.15, 195500),
    ('AXP', 62.58, '6/11/2007', '9:36am', -0.46, 935000),
    ]
    with open('aaa.csv','w') as f:
        f_csv = csv.writer(f)
        f_csv.writerow(headers)
        f_csv.writerows(rows)

  • 相关阅读:
    Count and Say
    Valid Sudoku
    Find First and Last Position of Element in Sorted Array
    Search in Rotated Sorted Array
    Longest Valid Parentheses
    web前端中文教程库
    三代基因组拼接软件--Falcon篇
    使用ThreadPoolExecutor并行执行独立的单线程任务
    python中的计时器:timeit
    Python教程
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349465.html
Copyright © 2011-2022 走看看