打开和保存文件.csv

推荐使用第3,4种方法打开保存.csv
1.使用csv打开

import csv
import os
with open(os.path.join(Path1,Path2), 'r') as f:
    reader = csv.reader(f)
    for row in reader:
    	print(row)

2.用python的自身语法打开

import os
with open(os.path.join(Path1,Path2), 'r') as f:
    for line in f:
        items = line.strip().split(',')
        print(items )

3.使用pandas打开pd.read_csv

import numpy as np
import pandas as pd
from pandas import DataFrame
 
filepath=os.path.join(Path1,Path2)
df=pd.read_csv(filepath,header=0,encoding="gbk")
print(df)

关于具体使用方法:https://www.cnblogs.com/wyy1480/p/10322336.html
3.使用pandas保存

import pandas as pd
 
list=[[1,2,3],[4,5,6],[7,9,9]]
# 下面这行代码运行报错
# list.to_csv('e:/testcsv.csv',encoding='utf-8')
name=['one','two','three']
test=pd.DataFrame(columns=name,data=list)#数据有三列,列名分别为one,two,three
print(test)
test.to_csv('e:/testcsv.csv',encoding='gbk')

4.保存为npy文件

np.save(file_to_save, list)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值