class: center, middle, inverse, title-slide # Python处理CSV文件数据 ## 🌱 ### 吴燕丰 ### 江西财大,金融学院 ### 2020/10/29 --- ### CSV格式数据 CSV: Comma-Seperated Values (逗号分隔值) [财经大学列表.csv](财经大学列表.csv) ``` 江西财经大学,江西,南昌 上海财经大学,上海,杨浦 西南财经大学,四川,成都 东北财经大学,辽宁,大连 ``` 也可以添加标题行,比如: ``` 学校名称,所在省市,城市或区 江西财经大学,江西,南昌 上海财经大学,上海,杨浦 西南财经大学,四川,成都 东北财经大学,辽宁,大连 ``` --- ### CSV module(模块) 读操作 `csv.reader()` ```python import csv with open('财经大学列表.csv', mode='r', newline='', encoding='utf-8') as csvfile: csvreader = csv.reader(csvfile) for row in csvreader: print(f'{row[0]}位于 {row[1]} 省/市 {row[2]} 市/区') ``` ``` ## 江西财经大学位于 江西 省/市 南昌 市/区 ## 上海财经大学位于 上海 省/市 杨浦 市/区 ## 西南财经大学位于 四川 省/市 成都 市/区 ## 东北财经大学位于 辽宁 省/市 大连 市/区 ``` .footnote[ **注意**:`newline=''`,`encoding='utf-8'`,`mode='r'` ] --- ### CSV module(模块) 写操作 `csv.writer()` ```python import csv with open('aNewFile.csv', mode='w', newline='', encoding='utf-8') as f: writer = csv.writer(f) writer.writerow(['吴燕丰','男','江西']) writer.writerow(['Maxwell','Male','Scotland']) writer.writerow(['Turing','Male','England']) writer.writerow(['Donald Knuth','Male','America']) writer.writerow(['Marie Curie','Female','Poland']) ``` ``` ## 10 ## 23 ## 21 ## 27 ## 27 ``` .footnote[ 提示:写一行用`writerow()`,写多行用`writerows()`,`mode='a'`表示追加写入,不覆盖已有内容 ] --- ### 文件读写 可参考网站: - [Python File(文件) 方法](https://www.runoob.com/python/file-methods.html) - [读写文件(Python官方文档)](https://docs.python.org/zh-cn/3/tutorial/inputoutput.html#reading-and-writing-files) 例子: ```python f = open('filename.txt', mode='r', encoding='utf-8') content = f.read() ``` --- ### DataFrame 与 CSV DataFrame是模块[pandas](https://pandas.pydata.org/pandas-docs/stable/user_guide/10min.html)的一个数据结构,需先安装模块pandas,然后使用它。 在Anaconda Prompt窗口输入 ```bash pip install pandas -i https://pypi.tuna.tsinghua.edu.cn/simple ``` - pandas 读 csv 文件 [pandas.read_csv()](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html#pandas.read_csv) ```python import pandas as pd df = pd.read_csv('aNewFile.csv', header=None) df.head() ``` ``` ## 0 1 2 ## 0 吴燕丰 男 江西 ## 1 Maxwell Male Scotland ## 2 Turing Male England ## 3 Donald Knuth Male America ## 4 Marie Curie Female Poland ``` --- ### DataFrame 与 CSV(续) - pandas 写 csv 文件 [pandas.DataFrame.to_csv](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html#pandas.DataFrame.to_csv) ```python import pandas as pd df = pd.DataFrame({ '姓名': ['吴燕丰','Maxwell','Turing','Donald Knuth','Marie Curie'], '性别': ['男','Male','Male','Male','Female'], '籍贯': ['江西','Scotland','England','America','Poland'] }) df.to_csv('saved_from_DataFrame.csv') ``` --- ### Python File Open **open()** There are four different methods (modes) for opening a file: |mode|meaning| |----|-------| |"r"| Read - Default value. Opens a file for reading, error if the file does not exist| |"a"| Append - Opens a file for appending, creates the file if it does not exist| |"w"| Write - Opens a file for writing, creates the file if it does not exist| |"x"| Create - Creates the specified file, returns an error if the file exists| In addition you can specify if the file should be handled as binary or text mode |mode|meaning| |----|-------| |"t"| Text - Default value. Text mode| |"b"| Binary - Binary mode (e.g. images)| --- ### Open a file in a different location If the file is located in a different location, you will have to specify the file path, like this: ### 🌰 Open a file on a different location: ```python f = open("D:\\myfiles\welcome.txt", "r") print(f.read()) ``` --- ### Read Only Parts of the File ### 🌰 Return the 5 first characters of the file: ```python f = open("demofile.txt", "r") print(f.read(5)) ``` ### 🌰 Read two lines of the file: ```python f = open("demofile.txt", "r") print(f.readline()) print(f.readline()) ``` --- ### Write to an Existing File To write to an existing file, you must add a parameter to the **open()** function: - "a" - Append - will append to the end of the file - "w" - Write - will overwrite any existing content **append** ```python f = open("demofile2.txt", "a") f.write("Now the file has more content!") f.close() f = open("demofile2.txt", "r") print(f.read()) ``` **overwrite** ```python f = open("demofile3.txt", "w") f.write("Woops! I have deleted the content!") f.close() f = open("demofile3.txt", "r") print(f.read()) ``` --- ### Create a New File To create a new file in Python, use the **open()** method, with one of the following parameters: - "x" - Create - will create a file, returns an error if the file exist - "a" - Append - will create a file if the specified file does not exist - "w" - Write - will create a file if the specified file does not exist Example Create a file called "myfile.txt": ```python f = open("myfile.txt", "x") ``` Create a new file if it does not exist: ```python f = open("myfile.txt", "w") ``` --- ### Python Delete File Remove the file "demofile.txt": ```python import os os.remove("demofile.txt") ``` ### Check if File exist Check if file exists, then delete it: ```python import os if os.path.exists("demofile.txt"): os.remove("demofile.txt") else: print("The file does not exist") ``` --- ### Delete Folder Remove the folder "myfolder": ```python import os os.rmdir("myfolder") ```