2017-02-23 8 views
2

私はuniVocity-parsersの助けを借りてGTFS.zipからCSVファイルを読み込もうとしています。何らかの理由で、一部のCSVファイルの最初の列が正しく解析されないようです。たとえば、次のようになります「stops.txt」ファイルに:uniVocityは、最初の列をBeanに解析しません。

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" 

「stop_id」フィールドが正しく解析されません。これは、私が「メソッドで値「ヌル」

を持っています

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

関連する問題