Task目录
1.函数关键字
2.函数的定义
3.函数参数与作用域
4.函数返回值
5.file
a. 打开文件方式(读写两种方式)
b. 文件对象的操作方法
c. 学习对excel及csv文件进行操作
6.os模块
1.函数关键字
2.函数的定义
3.函数参数与作用域
- 参数个数
- 参数传递
- 位置传递和名称传递
- 可选参数传递
-
可变参数传递
- 局部变量和全局变量
4.函数返回值
5.file
a. 打开文件方式(读写两种方式)
- 文件的打开
- 文件的打开模式
-
文件的编码方式
- 文件的关闭
- 文件内容的读取
b. 文件对象的操作方法
- 文件的全文本操作
- 文件的逐行操作
c. 学习对excel及csv文件进行操作
1、python读写csv文件
import csv
#读取csv文件内容方法1
csv_file = csv.reader(open('testdata.csv','r'))
next(csv_file, None) #skip the headers
for user in csv_file:
print(user)
#读取csv文件内容方法2
with open('testdata.csv', 'r') as csv_file:
reader = csv.reader(csv_file)
next(csv_file, None)
for user in reader:
print(user)
#从字典写入csv文件
dic = {'fengju':25, 'wuxia':26}
csv_file = open('testdata1.csv', 'w', newline='')
writer = csv.writer(csv_file)
for key in dic:
writer.writerow([key, dic[key]])
csv_file.close() #close CSV file
csv_file1 = csv.reader(open('testdata1.csv','r'))
for user in csv_file1:
print(user)
2、python读写excle文件
import xlrd, xlwt #xlwt只能写入xls文件
#读取xlsx文件内容
rows = [] #create an empty list to store rows
book = xlrd.open_workbook('testdata.xlsx') #open the Excel spreadsheet as workbook
sheet = book.sheet_by_index(0) #get the first sheet
for user in range(1, sheet.nrows): #iterate 1 to maxrows
rows.append(list(sheet.row_values(user, 0, sheet.ncols))) #iterate through the sheet and get data from rows in list
print(rows)
#写入xls文件
rows1 = [['Name', 'Age'],['fengju', '26'],['wuxia', '25']]
book1 = xlwt.Workbook() #create new book1 excle
sheet1 = book1.add_sheet('user') #create new sheet
for i in range(0, 3):
for j in range(0, len(rows1[i])):
sheet1.write(i, j, rows1[i][j])
book1.save('testdata1.xls') #sava as testdata1.xls
6.os模块
参考来源
Python语言程序设计:https://www.icourse163.org/course/BIT-268001
python中os模块用法:https://blog.youkuaiyun.com/weixin_39541558/article/details/79971971
python读写操作csv及excle文件:https://www.cnblogs.com/cnkemi/p/8671493.html
python中os模块函数方法详解最全最新:https://www.cnblogs.com/apollo1616/p/9510322.html