Python의 수집 정리: csv 모듈, xlrd 모듈

1399 단어

csv 모듈


쓰다

import csv
address = '\test.csv'  # 
# :
# 'w', encoding='utf8', newline=''                 
with open(address,'w',newline='') as fout:
    cout = csv.DictWriter(fout,['Name','SumCount','DropCount','OutCount','InCount']) #[]list CSV 
    cout.writeheader()   # 
    cout.writerows(data) #data list, dict, key 
    
# :
# :
cout = csv.writer(fout)
cout.writerows(data)

읽기

# :
with open(address,'r') as fin:
    cin = csv.reader(fin)
    out = [row for row in cin]
print(out)# , 

# :
# :
with open(address,'r') as fin:
    cin = csv.DictReader(fin,filenames=['first','last'])
    out = [row for row in cin]
print(out)

# :
with open(address,'r') as fin:
    cin = csv.DictReader(fin)
    out = [row for row in cin]
print(out)

xlrd 모듈


읽기

import xlrd
address = 'test.xls'

def read_xls():
    outdata = []
    data = xlrd.open_workbook(address)
    table = data.sheets()[0]
    title = table.row_values(0)

    for row in range(2, table.nrows):
        rn = [i.strip() for i in table.row_values(row)]
        d = {k: v for k, v in zip(title, rn)}
        #print(d)
        outdata.append(d)
    return outdata

좋은 웹페이지 즐겨찾기