我应该使用`with open(file):`if if`pd.read_csv`?
发布时间:2020-12-20 11:08:33 所属栏目:Python 来源:网络整理
导读:上下文 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
上下文
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文件时? 解决方法
使用open(‘<>‘)作为file:方法允许用户需要对文件中的单行或多行进行逐行操作.
pandas处理文件的方式不同.将文件导入到pandas dataframe时,它会将文件的全部内容导入到dataframe中.不需要指定打开和关闭文件,因为您将在那里处理数据框. 因此,当您将文件读取到pandas数据帧时,不需要使用open(). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |