2016-10-22 1 views
4

sys.stdin에서 입력을 가져 오려고합니다. 이것은 hadoop을위한지도 감속기 프로그램입니다. 입력 파일은 txt 형식입니다. 데이터 세트의 미리보기 : 내가 시도하고있다텍스트 파일 가져 오기 : 파일에서 구문 분석 할 열 없음

196 242 3 881250949 
186 302 3 891717742 
22 377 1 878887116 
244 51 2 880606923 
166 346 1 886397596 
298 474 4 884182806 
115 265 2 881171488 
253 465 5 891628467 
305 451 3 886324817 
6 86 3 883603013 
62 257 2 879372434 
286 1014 5 879781125 
200 222 5 876042340 
210 40 3 891035994 
224 29 3 888104457 
303 785 3 879485318 
122 387 5 879270459 
194 274 2 879539794 
291 1042 4 874834944 

코드 - 나는 또한 delimiter = \t, header=False,defining column name 아무것도 시도

import sys 
df = pd.read_csv(sys.stdin,error_bad_lines=False) 

가 작동하는 것 같다, 내가 점점 오전 오류이 오류는 다음과 같습니다

내가 파이썬 (하지 하둡)에서 직접이를하려고 할 때 경우
[[email protected] lab]# cat /root/lab/u.data | python /root/lab/mid-1-mapper.py |python /root/lab/mid-1-reducer.py 
Traceback (most recent call last): 
    File "/root/lab/mid-1-reducer.py", line 8, in <module> 
    df = pd.read_csv(sys.stdin,delimiter='\t') 
    File "/opt/rh/python27/root/usr/lib64/python2.7/site-packages/pandas/io/parsers.py", line 645, in parser_f 
    return _read(filepath_or_buffer, kwds) 
    File "/opt/rh/python27/root/usr/lib64/python2.7/site-packages/pandas/io/parsers.py", line 388, in _read 
    parser = TextFileReader(filepath_or_buffer, **kwds) 
    File "/opt/rh/python27/root/usr/lib64/python2.7/site-packages/pandas/io/parsers.py", line 729, in __init__ 
    self._make_engine(self.engine) 
    File "/opt/rh/python27/root/usr/lib64/python2.7/site-packages/pandas/io/parsers.py", line 922, in _make_engine 
    self._engine = CParserWrapper(self.f, **self.options) 
    File "/opt/rh/python27/root/usr/lib64/python2.7/site-packages/pandas/io/parsers.py", line 1389, in __init__ 
    self._reader = _parser.TextReader(src, **kwds) 
    File "pandas/parser.pyx", line 538, in pandas.parser.TextReader.__cinit__ (pandas/parser.c:5896) 
pandas.io.common.EmptyDataError: No columns to parse from file 

그러나, 그것을 잘 작동합니다. 유래 게시물에 모습에

내가 시도, 포스트 중 하나를 시도하고 제외를 제안했다. 적용하면 빈 파일이 나에게 남습니다. 아무도 도와 줄 수 있습니까? 감사합니다

답변

3

시도를 사용하고 당신이 오류에도 불구하고 계속 그들을 처리 할 수 ​​있습니다 제외. 마술처럼 실수를 수정하지 않아도됩니다.

read_csv

은 입력이 분명하지 csv 파일을 기대하고있다. 설명서에 대한 간략한 살펴보기 :

delim_whitespace : boolean, default False

Specifies whether or not whitespace (e.g. ' ' or ' ') will be used as the sep. Equivalent to setting sep='+s'. If this option is set to True, nothing should be passed in for the delimiter parameter.

올바른 인수로 보입니다. 사용

pandas.read_csv(filepath_or_buffer, delim_whitespace=True). 

탭이 확장되지 않는 delimiter='\t' 또한, 작업을해야 사용 (공백으로 대체). 우리가 정말로 알 수없는 것처럼 delim_whitespace이 더 좋은 옵션 인 것 같습니다. 이 방법으로 문제가 해결되지 않으면

, 당신이 제대로 텍스트를 통과하는지 확인하기 위해 sys.stdin을 인쇄 할 수 있습니다.

편집 : 난 그냥 당신이

cat /root/lab/u.data | python /root/lab/mid-1-mapper.py |python /root/lab/mid-1-reducer.py 

는이 방법을, mid-1-reducer.py 프로세스 mid-1-mapper.py의 출력을 목적으로 사용하는 것을보고 . u.data 파일의 내용을 처리하려면 sys.stdin이 아닌 파일을 읽는 것이 좋습니다.

+0

수입 SYS 경우 __name__ == '__main__'PD 로 수입 팬더 : DF = pd.read_csv (sys.stdin, 헤더 = 없음, delim_whitespace = 참) – mezz

+0

여전히 같은 오류가 df 명령 인쇄 . – mezz

+0

파이썬 스크립트에서'sys.stdin'을 출력 할 수 있습니까 ('mid-1-reducer.py'가 중요한 것입니까?)? 단순히'readlines' 함수 나 비슷한 것을 사용할 수 있습니다. 질문에 인쇄 한 입력과 동일한 출력을 제공합니까? – DerWeh

0

당신은 구분자로 공백 사용하는 True로 delim_whitespace을 설정해야합니다.

import sys 
import pandas as pd 

if __name__ == '__main__': 
    df = pd.read_csv(sys.stdin, header=None, delim_whitespace=True) 
    print df