python中有关文件的知识。

1、文件的读取

调用open()函数,返回一个File对象;   fp = open('D:/123/Test.txt', 'w')

调用File对象的read()或write()方法;     fp.write('HelloWorld')

调用File对象的close()方法,关闭该文件。  fp.close()
#目录操作
import os
#创建目录
os.mkdir('D:/Test/')
#删除目录
os.rmdir('D:/Test/')
2、将excel改为csv格式

# -*- coding: utf-8 -*-
"""
Created on Mon May 6 11:08:37 2019

@author: 小米
"""

import pandas as pd

def ExcelToCsv_1(StartName, SheetName, EndName):
grade = pd.read_excel(StartName, sheet_name=SheetName)
for i in range(len(grade.index)):
for j in range(1, len(grade.columns)):
if grade.iloc[i, j] == '优秀':
grade.iat[i, j] = 90
elif grade.iloc[i, j] == '良好':
grade.iat[i, j] = 80
elif grade.iloc[i, j] == '合格':
grade.iat[i, j] = 60
else:
grade.iat[i, j] = 0
grade.to_csv(EndName)


def ExcelToCsv_2(StartName, SheetName, EndName):

grade = pd.read_excel(StartName, sheet_name=SheetName)
Grade = grade.replace("优秀", "90")
Grade = Grade.replace("良好", "80")
Grade = Grade.replace("不合格", "60")
Grade = Grade.replace("合格", "60")
Grade = Grade.fillna(value = 0)
Grade.to_csv(EndName)
ExcelToCsv_2('D:/Users/小米/Desktop/Python成绩登记信计.xlsx', 'Sheet1', 'D:/Users/小米/Desktop/Python成绩登记信计.csv')
ExcelToCsv_1('D:/Users/小米/Desktop/Python成绩.xlsx','Sheet2' ,'D:/Users/小米/Desktop/Python成绩.csv')


 


 

 

posted @ 2019-05-06 11:31  gsd_tt  阅读(249)  评论(0编辑  收藏  举报