2017-02-23 855 views
2

uniVocity-parsers의 도움으로 GTFS.zip에서 CSV 파일을 읽으려는 시도가 있습니다. 알아낼 수없는 문제가 발생합니다. 어떤 이유로 CSV 파일의 첫 번째 열이 올바르게 구문 분석되지 않는 것 같습니다. 은 "stops.txt"파일의 예를 들면 다음과 같습니다이 :uniVocity가 첫 번째 열을 빈으로 구문 분석하지 않습니다.

는 "stop_id"필드가 "NULL"값을 가질 것이다 올바르게 구문 분석되지 않습니다
stop_id,stop_name,stop_lat,stop_lon,location_type,parent_station 
"de:3811:30215:0:6","Freiburg Stübeweg","48.0248455941735","7.85563688037231","","Parent30215" 
"de:8311:30054:0:1","Freiburg Schutternstraße","48.0236251356332","7.72434519425597","","Parent30054" 
"de:8311:30054:0:2","Freiburg Schutternstraße","48.0235446600679","7.72438739944883","","Parent30054" 

이 내가 '하는 방법입니다 파일 읽기 위해 사용하고 있습니다 :

public <T> List<T> readCSV(String path, String file, BeanListProcessor<T> processor) { 
    List<T> content = null; 
    try { 
     // Get zip file 
     ZipFile zip = new ZipFile(path); 
     // Get CSV file 
     ZipEntry entry = zip.getEntry(file); 
     InputStream in = zip.getInputStream(entry); 

     CsvParserSettings parserSettings = new CsvParserSettings(); 
     parserSettings.setProcessor(processor); 
     parserSettings.setHeaderExtractionEnabled(true); 

     CsvParser parser = new CsvParser(parserSettings); 
     parser.parse(new InputStreamReader(in)); 
     content = processor.getBeans(); 

     zip.close(); 
     return content; 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return content; 
} 

을 그리고 이것은 내 정지 클래스 모습입니다 같은 :

public class Stop { 
@Parsed 
private String stop_id; 
@Parsed 
private String stop_name; 
@Parsed 
private String stop_lat; 
@Parsed 
private String stop_lon; 
@Parsed 
private String location_type; 
@Parsed 
private String parent_station; 

public Stop() { 
} 

public Stop(String stop_id, String stop_name, String stop_lat, String stop_lon, String location_type, 
     String parent_station) { 
    this.stop_id = stop_id; 
    this.stop_name = stop_name; 
    this.stop_lat = stop_lat; 
    this.stop_lon = stop_lon; 
    this.location_type = location_type; 
    this.parent_station = parent_station; 
} 

// --------------------- Getter -------------------------------- 
public String getStop_id() { 
    return stop_id; 
} 

public String getStop_name() { 
    return stop_name; 
} 

public String getStop_lat() { 
    return stop_lat; 
} 

public String getStop_lon() { 
    return stop_lon; 
} 

public String getLocation_type() { 
    return location_type; 
} 

public String getParent_station() { 
    return parent_station; 
} 

// --------------------- Setter -------------------------------- 
public void setStop_id(String stop_id) { 
    this.stop_id = stop_id; 
} 

public void setStop_name(String stop_name) { 
    this.stop_name = stop_name; 
} 

public void setStop_lat(String stop_lat) { 
    this.stop_lat = stop_lat; 
} 

public void setStop_lon(String stop_lon) { 
    this.stop_lon = stop_lon; 
} 

public void setLocation_type(String location_type) { 
    this.location_type = location_type; 
} 

public void setParent_station(String parent_station) { 
    this.parent_station = parent_station; 
} 

@Override 
public String toString() { 
    return "Stop [stop_id=" + stop_id + ", stop_name=" + stop_name + ", stop_lat=" + stop_lat + ", stop_lon=" 
      + stop_lon + ", location_type=" + location_type + ", parent_station=" + parent_station + "]"; 
    } 
} 

내가 호출하는 경우 내가 수정되지 않습니다 출력을 얻을 방법 :

PartialReading pr = new PartialReading(); 
    List<Stop> stops = pr.readCSV("VAGFR.zip", "stops.txt", new BeanListProcessor<Stop>(Stop.class)); 
    for (int i = 0; i < 4; i++) { 
     System.out.println(stops.get(i).toString()); 
    } 

출력 :

이런 이유
Stop [stop_id=null, stop_name=Freiburg Stübeweg, stop_lat=48.0248455941735, stop_lon=7.85563688037231, location_type=null, parent_station=Parent30215] 
Stop [stop_id=null, stop_name=Freiburg Schutternstraße, stop_lat=48.0236251356332, stop_lon=7.72434519425597, location_type=null, parent_station=Parent30054] 
Stop [stop_id=null, stop_name=Freiburg Schutternstraße, stop_lat=48.0235446600679, stop_lon=7.72438739944883, location_type=null, parent_station=Parent30054] 
Stop [stop_id=null, stop_name=Freiburg Waltershofen Ochsen, stop_lat=48.0220902613143, stop_lon=7.7205756507492, location_type=null, parent_station=Parent30055] 

사람은 알고 있나요 내가 고칠 수있는 방법? 이것은 또한 내가 테스트 한 "routes.txt"및 "trips.txt"파일에서도 발생합니다. 이것은 GTFS 파일입니다. http://stadtplan.freiburg.de/sld/VAGFR.zip

답변

0

헤더를 인쇄하면 첫 번째 열이 올바르게 보이지 않습니다. 이는 UTF-8을 사용하여 BOM 마커을 사용하여 인코딩 된 파일을 구문 분석하기 때문입니다.

기본적으로 파일은 인코딩이 무엇인지 나타내는 몇 바이트로 시작됩니다. 파서는 내부적으로 처리하지 않습니다,하지만 당신은 단지 올바른 출력을 얻기 위해이 바이트를 건너 뛸 수 있습니다 : 위의 해킹이 작동

//... your code here 
ZipEntry entry = zip.getEntry(file); 
InputStream in = zip.getInputStream(entry); 

if(in.read() == 239 & in.read() == 187 & in.read() == 191){ 
    System.out.println("UTF-8 with BOM, bytes discarded"); 
} 

CsvParserSettings parserSettings = new CsvParserSettings(); 
//...rest of your code here 

을,하지만 당신은 가공-IO는 편의를 위해 BOMInputStream과 이상을 제공 사용할 수 있습니다 이런 종류의 일을 깨끗이 처리하십시오.

희망이 있습니다.

+0

감사합니다. 저에게 문제가 해결되었습니다. – Kazanagi