I've learned,当在Python中读取文件时应使用with 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)
但是,我已经看到多个示例,这些示例在使用熊猫的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)
➥使用熊猫的with open():
读取.csv
文件时,我应该使用pd.read_csv
吗?
(或者pd.read_csv
已经足够聪明了?)
答案 0 :(得分:4)
MATCH (t:Title)-[:ACTS_IN]-(p:Principal)-[:ACTS_BY]->(n:Name)
WHERE t.originalTitle IN ["The Terminator", "True Lies"]
WITH DISTINCT t, p, n
WITH n, count(n) as occurrences
WHERE occurrences >= 2
RETURN n.primaryName
方法允许用户需要对文件中的单行或多行进行逐行操作。
with open('<>') as file:
以不同的方式处理文件。 将文件导入pandas数据框时,会将文件的全部内容导入数据框。不需要指定打开和关闭文件,因为您将在其中处理数据框。
因此, 当您将文件读取到pandas
时,不需要pandas dataframe
。
答案 1 :(得分:3)
pd.read_csv()非常聪明,可以处理文件的打开。足以区分文件对象和文件路径。
答案 2 :(得分:2)
将文件导入熊猫时,无需专门打开文件。您可以直接导入pandas数据框并开始使用数据框。
exam_data = pd.read_csv('exams.csv', quotechar='"')
exam_data
所以回答您的问题pd.read_csv就足够了。