2017-05-14 2 views
2

xlsx 파일을 파이썬으로 가져 오는 것에 대한 기본적인 질문이 있습니다. 동일한 주제에 대해 여러 가지 응답을 확인했지만, 필자가 시도하던 파일을 파이썬으로 가져올 수는 없습니다. 여기 내 코드는 에러가 나는 나타날 수excel 파일을 파이썬으로 가져 오기

import pandas as pd 

import xlrd 

file_location = 'C:\Users\cagdak\Desktop\python_self_learning\Coursera\sample_data.xlsx' 
workbook = xlrd.open_workbook(file_location) 

오류 : 팬더와

IOError: [Errno 2] No such file or directory: 'C:\\Users\\cagdak\\Desktop\\python_self_learning\\Coursera\\sample_data.xlsx' 
+0

당신의 문제는 파일이 있는지 찾을 수 없으며 가져 오기에 문제가 없습니다. 파일은 당신이 생각하는 경로에 있습니다. – boardrider

+0

힌트 : xlsx 파일이 해당 위치에 있는지 확인 했습니까? – karthikr

+0

예. C : \ Users \ cagdak \ Desktop \ python_self_learning \ Coursera 폴더 경로를 여기에 복사하고 Excel 파일의 이름은 다음과 같습니다. sample_data –

답변

4

가 직접 엑셀 파일의 열을 얻을 수 있습니다. 여기에 코드가 있습니다.

import pandas 
df = pandas.read_excel('sample.xls') 

#print the column names 
print df.columns 

#get the values for a given column 
values = df['collumn_name'].values 

#get a data frame with selected columns 
FORMAT = ['Col_1', 'Col_2', 'Col_3'] 
df_selected = df[FORMAT] 
+0

감사하지만 여전히 "IOError : [Errno 2] No such file or directory"오류가 발생합니다. 나는 파일 위치와 파일 이름이 정확하다는 것을 100 % 확신하지만. 나는 이것을하기 위해 거의 3 시간을 보냈다. –

0

당신은 예를 들어, 대신 raw strings or escape your backslash을 사용해야합니다

file_location = r'C:\Users\cagdak\Desktop\python_self_learning\Coursera\sample_data.xlsx' 

또는

file_location = 'C:\\Users\\cagdak\\Desktop\python_self_learning\\Coursera\\sample_data.xlsx' 
+0

나는 이것들을 시험해 보았지만 여전히 같은 오류가 발생했습니다 ... –

+0

@CagdasKanar 파일에 대한 읽기 권한이 있습니까? ''python -c "import os; print (os.stat (r'C : \ Users \ cagdak \ Desktop \ python_self_learning \ Coursera \ sample_data.xlsx '))를 실행하면 어떻게됩니까? – andy

+0

노트북이나 터미널에서 이걸 실행해야합니까? –

0

는 가서이 시도 :

file_location = 'C:/Users/cagdak/Desktop/python_self_learning/Coursera/sample_data.xlsx' 
+0

이 질문에 대한 답을 제공하지 않습니다. 충분한 [평판] (https://stackoverflow.com/help/whats-reputation)이 있으면 [모든 게시물에 주석 달기] (https://stackoverflow.com/help/privileges/comment) 할 수 있습니다. 대신, [질문자의 설명이 필요없는 답변을 제공하십시오] (https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can- i-do- 대신). - [검토 중] (리뷰/저품절 게시물/19024737) – Fawzan

+0

코드에 세부 정보를 추가하십시오. –

관련 문제