2017-05-22 3 views
2

csv 파일로 pandas.DataFrame을 만들어야합니다. 이를 위해 pandas.csv_reader(...) 메서드를 사용하고 있습니다. 이 파일의 문제점은 하나 이상의 열에 값 (파일 형식을 제어하지 않음) 내에 쉼표가 포함되어 있다는 것입니다. 나는이 question에서 솔루션을 구현하기 위해 노력하고 있지만 다음과 같은 오류 얻을 : 나는 정부가 비어 시도 csv 파일이 솔루션을 구현 한 후 몇 가지 이유를 들어쉼표로 팬더에서 CSV 파일 구문 분석

pandas.errors.EmptyDataError: No columns to parse from file 

합니다. 여기

나는 코드를 사용하고 있습니다 :

# fix csv file 
with open ("/Users/username/works/test.csv",'rb') as f,\ 
open("/Users/username/works/test.csv",'wb') as g: 
    writer = csv.writer(g, delimiter=',') 
    for line in f: 
     row = line.split(',', 4) 
     writer.writerow(row) 
# Manipulate csv file 
data = pd.read_csv(os.path.expanduser\ 
("/Users/username/works/test.csv"),error_bad_lines=False) 

어떤 아이디어?

데이터 개요 :

Id0 Id 1 Id 2 Country Company Title  Email     
    23 123  456 AR  name cargador [email protected]     

    24 123  456 AR  name Executive assistant [email protected]     

    25 123  456 AR  name Asistente Administrativo [email protected]     

    26 123  456 AR  name Atención al cliente vía telefónica vía online [email protected]    
    39 123  456 AR  name Asesor de ventas [email protected]     

    40 123  456 AR  name inc. International company representative [email protected]    
    41 123  456 AR  name Vendedor de campo [email protected]     

    42 123  456 AR  name PUBLICIDAD ATENCIÓN AL CLIENTE [email protected]    
    43 123  456 AR  name Asistente de Marketing [email protected]     

    44 123  456 AR  name SOLDADOR [email protected]     
    217 123  456 AR  name Se requiere vendedores  Loja Quevedo  Guayas) [email protected] 
    218 123  456 AR  name Ing. Civil recién graduado Yaruquí [email protected]    
219 123  456 AR  name ayudantes enfermeria [email protected]     

220 123  456 AR  name Trip Leader for International Youth Exchange [email protected]     
221 123  456 AR  name COUNTRY MANAGER/DIRECTOR COMERCIAL [email protected]     
250 123  456 AR  name Ayudante de Pasteleria [email protected] Asesor [email protected] [email protected]  

사전 분석 CSV :

#,Id 1,Id 2,Country,Company,Title,Email,,,, 
23,123,456,AR,name,cargador,[email protected],,,, 
24,123,456,AR,name,Executive assistant,[email protected],,,, 
25,123,456,AR,name,Asistente Administrativo,[email protected],,,, 
26,123,456,AR,name,Atención al cliente vía telefónica , vía online,[email protected],,, 
39,123,456,AR,name,Asesor de ventas,[email protected],,,, 
40,123,456,AR,name, inc.,International company representative,[email protected],,, 
41,123,456,AR,name,Vendedor de campo,[email protected],,,, 
42,123,456,AR,name,PUBLICIDAD, ATENCIÓN AL CLIENTE,[email protected],,, 
43,123,456,AR,name,Asistente de Marketing,[email protected],,,, 
44,123,456,AR,name,SOLDADOR,[email protected],,,, 
217,123,456,AR,name,Se requiere vendedores,, Loja , Quevedo, Guayas),[email protected] 
218,123,456,AR,name,Ing. Civil recién graduado, Yaruquí,[email protected],,, 
219,123,456,AR,name,ayudantes enfermeria,[email protected],,,, 
220,123,456,AR,name,Trip Leader for International Youth Exchange,[email protected],,,, 
221,123,456,AR,name,COUNTRY MANAGER/DIRECTOR COMERCIAL,[email protected],,,, 
250,123,456,AR,name,Ayudante de Pasteleria,[email protected], Asesor,[email protected],[email protected], 
251,123,456,AR,name,Ejecutiva de Ventas,[email protected],,,, 
+2

당신이 파일의 데이터에 대한 개요를 추가 할 수 있습니까? –

+0

데이터 개요를 추가했습니다. 감사합니다 – David

+2

@ChihebNexus가 미리 파싱 된 CSV 데이터를 요구했기 때문에이를 올바르게 구문 분석하는 방법을 볼 수 있습니다. – pshep123

답변

2

당신은 어떤 쉼표가 공간 다음에, 그 모든 것을의의 comapny에 대한 것을 가정 할 경우 나머지 쉼표가 전자 메일 주소보다 먼저 열에 있으면 작은 구문 분석기가이를 처리 할 수 ​​있습니다.

코드 :

import csv 
import re 

VALID_EMAIL = re.compile(r'[^@][email protected][^@]+\.[^@]+') 

def read_my_csv(file_handle): 
    # build csv reader 
    reader = csv.reader(file_handle) 

    # get the header, and find the e-mail and title columns 
    header = next(reader) 
    email_column = header.index('Email') 
    title_column = header.index('Title') 

    # yield the header up to the e-mail column 
    yield header[:email_column+1] 

    # for each row, go through rebuild columns 
    for row in reader: 

     # for each row, put the Company column back together 
     while row[title_column].startswith(' '): 
      row[title_column-1] += ',' + row[title_column] 
      del row[title_column] 

     # for each row, put the Title column back together 
     while not VALID_EMAIL.match(row[email_column]): 
      row[email_column-1] += ',' + row[email_column] 
      del row[email_column] 
     yield row[:email_column+1] 

시험 코드 :

with open ("test.csv", 'rU') as f: 
    generator = read_my_csv(f) 
    columns = next(generator) 
    df = pd.DataFrame(generator, columns=columns) 

print(df) 

결과 :

 # Id 1 Id 2 Country  Company \ 
0 23 123 456  AR  name 
1 24 123 456  AR  name 
2 25 123 456  AR  name 
3 26 123 456  AR  name 
4 39 123 456  AR  name 
5 40 123 456  AR name, inc. 
6 41 123 456  AR  name 
7 42 123 456  AR  name 
8 43 123 456  AR  name 
9 44 123 456  AR  name 
10 217 123 456  AR  name 
11 218 123 456  AR  name 
12 219 123 456  AR  name 
13 220 123 456  AR  name 
14 221 123 456  AR  name 
15 250 123 456  AR  name 
16 251 123 456  AR  name 

               Title   Email 
0           cargador [email protected] 
1        Executive assistant [email protected] 
2       Asistente Administrativo [email protected] 
3 Atención al cliente vía telefónica , vía online [email protected] 
4         Asesor de ventas [email protected] 
5    International company representative [email protected] 
6         Vendedor de campo [email protected] 
7     PUBLICIDAD, ATENCIÓN AL CLIENTE [email protected] 
8        Asistente de Marketing [email protected] 
9           SOLDADOR [email protected] 
10 Se requiere vendedores,, Loja , Quevedo, Guayas) [email protected] 
11    Ing. Civil recién graduado, Yaruquí [email protected] 
12        ayudantes enfermeria [email protected] 
13  Trip Leader for International Youth Exchange [email protected] 
14    COUNTRY MANAGER/DIRECTOR COMERCIAL [email protected] 
15       Ayudante de Pasteleria [email protected] 
16        Ejecutiva de Ventas [email protected] 
+0

고맙습니다. 스크립트가 회사 열이 제목과 섞여있는 경우를 파악할 수있는 변경 사항이 있습니까? 이 예는 인덱스 5입니다. 다음과 같아야합니다. company : name inc. 직함 : 국제 회사 대표 감사! – David

+1

@David, ','는 필드 구분 기호이므로 회사와 제목을 구별 할 수있는 논리를 만들어야합니다. 나는 앞 부분을 앞의 행과 함께 사용한다고 가정하여 회사를 수정하기 위해 약간 추가했다. 위에서 제안한 휴리스틱은 분명히 단순합니다. 위의 설명에서 psheps 포인트입니다. 데이터 정리는 거의 항상 예술의 비트로, 데이터를 잘못 처리하는 방식에 크게 의존합니다. –