【Python】csv文件的讀寫(pandas,csv兩種方式)

在使用python處理數據的過程中,經常需要做一些數據讀取和寫入的工作,比較常用的數據格式是csv,csv文件是一種以逗號分割字符的文件形式

例如:demo.csv,一個很簡單的csv文件
name,score
alex,1
jon,2
sansa,3

讀寫csv文件常用的有兩種方式,一種是使用csv,一種是使用pandas

##1 使用pandas進行讀寫

pandas 是一個非常好用的工具,能夠按照格式讀入和方便的寫出csv文件,下面是一個簡單的讀文件的例子:

import pandas as pd

file = pd.read_csv('demo.csv')
df = pd.DataFrame(file)

print(df)

output:
title score 0 alex 1 1 jon 2 2 sansa 3

如果想要一行一行的讀取dataframe中的內容:

import pandas as pd

file = pd.read_csv('demo.csv')
df = pd.DataFrame(file)

for i in range(len(df)):
    document = df[i:i+1]
    print(document,'\n')

output:
title score
0 alex 1

title score
1 jon 2

title score
2 sansa 3

要取到document中的每個值:

for i in range(len(df)):
    document = df[i:i+1]
    title = document['title'][i]
    score = document['score'][i]
    print(title,score,'\n')

output:
alex 1

jon 2

sansa 3

使用pandas將dictionary寫入csv文件,按列寫入

import pandas as pd

file = pd.read_csv('demo.csv')
df = pd.DataFrame(file)

dict = {}
for i in range(len(df)):
    document = df[i:i+1]
    title = document['title'][i]
    score = document['score'][i]
    dict[title] = score

new_df = pd.DataFrame.from_dict(dict,orient='index')
new_df.to_csv('pandas_new.csv')

結果文件pandas_new.csv
,0
alex,1
jon,2
sansa,3

Plus:一個不錯的介紹鏈接

##2 使用csv進行讀寫

import csv

with open('demo.csv', 'r') as csvfile:
    spamreader = csv.reader(csvfile)
    for row in spamreader:
        print(row)# type of row: list

output:
['title,score'] ['alex,1'] ['jon,2'] ['sansa,3']

Hints:
When you read the csv file, if you use ‘rb’, means Read the file in Binary mode.
while if you use ‘rt’, means Read the file in Text mode.
I usually just use ‘r’.

#####寫

import csv

with open('new.csv','w') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(['country','language'])#這裏要以list形式寫入,writer會在新建的csv文件中,一行一行寫入

output:
csv文件,content:
country,language

Plus:如果想要以dictionary的形式讀取和寫入,csv也提供了DictReader,DIctWriter的方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章