我正在尝试读取CSV文件。我在两个文件夹下有两个CSV。我得到了CSV文件,但是当我尝试读取内容时,它说我没有这样的文件或目录。
import os
import csv
def scanCSVFile(folder, output_file_name):
myfile = open(output_file_name, "a")
files = os.listdir(folder)
for file in files:
if file.endswith('.csv'): #gives two csv files
with open(file) as csvfile:
csvreader = csv.reader(csvfile)
next(csvreader)
for line in csvreader:
print (line)
myfile.close()
def openCSV(path):
scanCSVFile("./src/goi","goi.yml")
scanCSVFile("./src/kanji","kanji.yml")
openCSV('.')
错误:
C:\Users\Peace\Documents\LMS>python csvToYaml.py
Traceback (most recent call last):
File "csvToYaml.py", line 26, in <module> openCSV('.')
File "csvToYaml.py", line 24, in openCSV scanCSVFile("./src/goi","goi.yml")
File "csvToYaml.py", line 14, in scanCSVFile with open(file) as csvfile:
FileNotFoundError: [Errno 2] No such file or directory: 'Pro.csv'
答案 0 :(得分:1)
您必须从代码中删除中断
import os,glob
folder_path = r'path'
for filename in glob.glob(os.path.join(folder_path, '*.csv')):
with open(filename, 'r') as f:
for line in f:
print(line)
答案 1 :(得分:0)
您可以使用pandas从csv文件读取数据。数据将存储在数据框中。 “ .yml”扩展名现在已过时。通常使用“ .yaml”。 以下链接显示了如何读取.yaml文件。 How can I parse a YAML file in Python
# loading libraries and reading the data from csv files.
import pandas as pd
market_df = pd.read_csv("./src/goi.csv")
print(market_df)