【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')

使用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的方法。

  • 13
    点赞
  • 73
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值