Skip to content

Instantly share code, notes, and snippets.

@smeghead
Created January 26, 2021 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smeghead/c7dbba6c4c65f5cf8196a08aa4ec8166 to your computer and use it in GitHub Desktop.
Save smeghead/c7dbba6c4c65f5cf8196a08aa4ec8166 to your computer and use it in GitHub Desktop.
rust-sandbox-read-csv
use std::env;
use std::fs::File;
use std::io::prelude::*;
#[derive(Debug)]
struct Organization<'a> {
id: &'a str,
name: &'a str,
}
fn main() {
let args: Vec<String> = env::args().collect();
let filename = &args[1];
// 現在の団体シートから読み込む
let mut f = File::open(filename).expect("file not found.");
let mut contents = String::new();
f.read_to_string(&mut contents).expect("read error.");
let mut _organizations: Vec<Organization> = Vec::new();
for line in contents.split("\n") {
let cols: Vec<&str> = line.split(",").collect();
if cols.len() < 5 {
continue;
}
if cols[2] == "" {
continue;
}
_organizations.push(Organization{ id: cols[0], name: cols[2]});
}
println!("line: {:?}", _organizations);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment