python 读取.csv文件
时间: 2023-08-09 13:11:15 浏览: 115
python读取csv文件
5星 · 资源好评率100%
要在Python中读取.csv文件,你可以使用pandas库或csv库。
使用pandas库的方法如下所示:
```python
import pandas as pd
# 使用read_csv()方法读取csv文件
df = pd.read_csv('file.csv')
# 打印数据框
print(df)
```
使用csv库的方法如下所示:
```python
import csv
# 打开csv文件
with open('file.csv', 'r') as f:
# 创建csv读取器
reader = csv.reader(f)
# 遍历每一行数据
for row in reader:
# 打印每一行数据
print(row)
```
请注意,如果你使用csv库读取csv文件,你需要手动处理列名和编码问题。而使用pandas库可以更方便地处理这些问题。
#### 引用[.reference_title]
- *1* [python读取csv文件的几种方式(含实例说明)](https://blog.csdn.net/qq_43160348/article/details/124331781)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [python读取csv文件](https://blog.csdn.net/qq_22464799/article/details/128023434)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [【Python】Python读取CSV文件](https://blog.csdn.net/qilei2010/article/details/115292696)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文