文件操作

csv

import csv
csvFile = open("csvData.csv", "w")            #创建csv文件
writer = csv.writer(csvFile)                  #创建写的对象
#先写入columns_name                             
writer.writerow(["index","a_name","b_name"])     #写入列的名称
#写入多行用writerows                                #写入多行
writer.writerows([[1,a,b],[2,c,d],[3,d,e]])
csvFile.close()

import pandas as pd
columns = ['image_id', 'Prediction']
dictory = {'image_id': img_name, 'Prediction': Prediction}
dataframe = pd.DataFrame(dictory, columns=columns)
dataframe.to_csv("submit/submit_test.csv", index=False, sep=',')
df = pd.read_csv('filename')
df = pd.read_csv('filename', header=None)
df = pd.read_csv('filename', header=None, names=['code', 'name'])
classlist=classid['code'].tolist()
namelist=classid['name'].tolist()

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
open(filename,'wb')

pkl

import pickle
file = './second_stage_benchmark.pkl' 
f = open(file,'rb') 
data = pickle.load(f)     

json

json.load(open(test_json_path, "rb"))

txt

python读取、写入txt文本内容

路径

os.path.split() #分离文件名
os.path.splitext() #分离后缀
os.path,join()

In [1]: import os                                                                                                                                                
In [2]: f = '1/2/3.4.5'                                                                                                                                            
In [3]: os.path.split(f)                                                                                                                                           
Out[3]: ('1/2', '3.4.5')
In [4]: os.path.splitext(f)                                                                                                                                        
Out[4]: ('1/2/3.4', '.5')

文件复制

import shutil
old_path = ''
new_path = ''
shutil.copyfile(old_path, new_path)

 

 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值