I’ve learned在Python中读取文件时应该使用open:
import csvwith 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已经够聪明了吗?)
pandas处理文件的方式不同.将文件导入到pandas dataframe时,它会将文件的全部内容导入到dataframe中.不需要指定打开和关闭文件,因为您将在那里处理数据框.
因此,当您将文件读取到pandas数据帧时,不需要使用open().
总结以上是内存溢出为你收集整理的我应该使用`with open(file):`if if`pd.read_csv`?全部内容,希望文章能够帮你解决我应该使用`with open(file):`if if`pd.read_csv`?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)