深度阅读

How to read a CSV file in Python?

作者
作者
2023年08月22日
更新时间
7.03 分钟
阅读时间
0
阅读量

To read a CSV file in Python, you can use the csv library or the pandas library. Here are two examples using both methods:

  1. Using the csv library:

import csv

with open(‘file.csv’, ‘r’) as file:
reader = csv.reader(file)
for row in reader:
print(row)


2. Using the `pandas` library:

import pandas as pd

df = pd.read_csv(‘file.csv’)
print(df)



Both methods involve opening the CSV file and reading its contents, but the `csv` library is more low-level and provides fine-grained control over reading and writing CSV files. Meanwhile, the `pandas` library provides a high-level interface to read, manipulate, and analyze CSV data using a `DataFrame` object.

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。