csv轉json


#-*- coding:utf-8 -*-

import os
import os.path
import csv
rootdir = "/Users/ying/Documents"     # folder stores csv files

for parent,dirnames,filenames in os.walk(rootdir):
    for filename in filenames:
        abs_path = os.path.join(parent,filename)
        if ".csv" in abs_path:
            print abs_path
            #對每個文件進行處理
            with open(abs_path, 'rb') as csvfile:
                reader = csv.reader(csvfile)
                rows = [row for row in reader]
                header = rows[0]
                for rowOne in rows[1:]:
                    json_row = {}
                    for i in range(0,len(rowOne)):
                        json_row[header[i]] = rowOne[i]
                    print json_row

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM