Skip to main content

from_csv

Function from_csv 

Source
pub fn from_csv<H: HashTable, P: AsRef<Path>>(
    hash_table: H,
    path: P,
    header_delimiter: Option<char>,
) -> Result<JMapInfo<H>>
Expand description

Read a JMapInfo from a CSV file

The CSV format uses a header row where each column is formatted as: FieldName:Type:DefaultValue

For example: ScenarioNo:Int:0,ZoneName:String:0 The delimiter between the parts can be customized (default is ‘:’) and should not appear in field names or type names

§Arguments

  • hash_table - The hash table to use for field name lookups. Field names from the CSV will be added to this hash table
  • path - The path to the CSV file to read
  • header_delimiter - Optional character that separates field name, type, and default value in the header. Default is ‘:’

§Returns

A JMapInfo populated with fields and entries from the CSV file

Examples found in repository?
examples/write_bcsv.rs (line 11)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    let lookup_path = Path::new("assets/strings_SMG.txt");
8    let hash_table = smg_hash_table_with_lookup(lookup_path)?;
9
10    let csv_path = Path::new("assets/examples/scenariodata.csv");
11    let jmap = from_csv(hash_table, csv_path, None)?;
12
13    println!("CSV Info");
14    println!("Entries: {}", jmap.len());
15    println!("Fields: {}", jmap.num_fields());
16    println!();
17
18    to_file(&jmap, "test_output.bcsv", &IoOptions::default())?;
19    println!("\nExported to test_output.bcsv");
20
21    Ok(())
22}