我应该使用`with open(file):`if if`pd.read_csv`?

上下文

I’ve learned在Python中读取文件时应该使用open:

import csv

with open('employee_birthday.txt') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        if line_count == 0:
            print(f'Column names are {", ".join(row)}')
            line_count += 1
        else:
            print(f'\t{row[0]} works in the {row[1]} department, and was born in {row[2]}.')
            line_count += 1
    print(f'Processed {line_count} lines.')

(source)

但是,我已经看到了使用pandas的pd.read_csv时未使用此结构的多个示例:

# Load the Pandas libraries with alias 'pd' 
import pandas as pd 
# Read data from file 'filename.csv' 
# (in the same directory that your python process is based)
# Control delimiters, rows, column names with read_csv (see later) 
data = pd.read_csv("filename.csv") 
# Preview the first 5 lines of the loaded data 
data.head()

(source)

➥我应该使用open():当使用pandas的pd.read_csv读取.csv文件时?
(或者pd.read_csv已经够聪明了吗?)

最佳答案 使用open(‘<>‘)作为file:方法允许用户需要对文件中的单行或多行进行逐行操作.

pandas处理文件的方式不同.将文件导入到pandas dataframe时,它会将文件的全部内容导入到dataframe中.不需要指定打开和关闭文件,因为您将在那里处理数据框.

因此,当您将文件读取到pandas数据帧时,不需要使用open().

点赞