如何读取从 os.listdir 获得的文件的内容?
How to read a content of a file obtained from os.listdir?
我正在尝试读取 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'
您可以使用 pandas.Data 从 csv 文件中读取数据,这些数据将存储在数据帧中。
“.yml”扩展名现已过时。大多数情况下使用“.yaml”。
下面的 link 显示了如何读取 .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)
您必须从代码中删除中断
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)
我正在尝试读取 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'
您可以使用 pandas.Data 从 csv 文件中读取数据,这些数据将存储在数据帧中。 “.yml”扩展名现已过时。大多数情况下使用“.yaml”。 下面的 link 显示了如何读取 .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)
您必须从代码中删除中断
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)