使用正确的代码和文件扩展名CSV时如何解决“ FileNotFoundError”?

时间:2019-04-26 10:12:38

标签: csv opencsv

我正在尝试在python中打开扩展名为.csv的文件,但是一直说找不到该文件。我正在从侧边栏复制路径,所以我不认为这是问题所在

我试图在文件路径之前插入/和./ 在文件名前加上r

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt 
from scipy.optimize import curve_fit

bkgrnd = pd.read_csv('/Desktop/Sro/Natrium22.csv')

无论我尝试了什么,它总是说FileNotFoundError

1 个答案:

答案 0 :(得分:0)

如果文件将始终为csv,则可以导入.csv

import csv

with open('C:\Users\user\Desktop\Sro\Natrium22.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:

在Windows上的规范,它需要规范化您的路径名,也许就是这个问题, 尝试做,肯定会起作用,

import os
import pandas as pd

cwd = os.getcwd()
filePath = 'C:/Users/user/Desktop/Sro/Natrium22.csv'
data = pd.read_csv(os.path.normcase(os.path.join(cwd, filePath)))
print(data)

您甚至可以尝试

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt 
from scipy.optimize import curve_fit

bkgrnd = pd.read_csv(r'C:\Users\user\Desktop\Sro\Natrium22.csv')
print(bkgrnd)