#csv #read-write #read #write #file #file-read #parse

pro_csv

易于使用的 CSV 读取/写入器

2 个版本

0.1.1 2023 年 3 月 1 日
0.1.0 2023 年 2 月 28 日

#20 in #file-read


用于 2 crates

MIT 许可证

20KB
455 代码行,不包括注释

Pro-CSV

易于使用的 CSV 读写库。

功能列表

  • 按索引读取
  • 在索引处设置
  • 使用标题查找行和列
  • 插入和追加行/列
  • 删除行/列
  • 调整表格大小
  • 保存到文件

示例

代码

fn main() {
    let mut csv = pro_csv::CSV::default();
    csv.load("a;b;c;d\na;b;c;d;d;d;d;d;d;d;\n"); // csv.load_from_file("demo.csv") exist

    // Debug & Output

    println!("# Debug\n\n{:?}", csv);
    println!("\n# Out\n\n{}", csv.to_string());

    // Reading

    println!(
        "\n# Column with name\n\n{:?}",
        csv.find_columns_index_with_name("a")
    );
    println!(
        "\n# Column content\n\n{:?}",
        csv.get_all_element_of_column(csv.find_column_index_with_name("a").unwrap())
    );
    println!(
        "\n# Line with name\n\n{:?}",
        csv.find_lines_index_with_name("a")
    );
    println!(
        "\n# Line that contain something\n\n{:?}",
        csv.find_lines_index_that_contains("a")
    );
    println!(
        "\n# Line content\n\n{:?}",
        csv.get_all_element_of_line(csv.find_line_index_with_name("a").unwrap())
    );
    println!(
        "\n# Line content using contains\n\n{:?}",
        csv.get_all_element_of_line(csv.find_line_index_that_contains("a").unwrap())
    );

    println!("\n# Get at specific location\n\n{:?}", csv.get(1, 1));
    println!(
        "\n# Get at specific location with headers\n\n{:?}",
        csv.get_with_headers(0, 0)
    );

    println!("\n# Get line count\n\n{:?}", csv.get_line_count());
    println!("\n# Get column count\n\n{:?}", csv.get_column_count());

    // Insert

    println!();

    println!("# Insert line");
    csv.insert_line(0, vec![String::from("insert_line")]);
    println!("# Insert column");
    csv.insert_column(0, String::from("insert_column"));

    // Append

    println!("# Append line");
    csv.append_line(vec![String::from("append_line")]);
    println!("# Append column");
    csv.append_column(String::from("append_column"));

    println!("# Correct the size");
    csv.correct_size();

    // Set

    println!("# Set an element");
    // Tips: You can set an element out of the current table
    // it will append lines/columns until it have the good size
    csv.set(0, 0, String::from("SET"));

    // Swap

    println!("# Swap two lines");
    csv.swap_lines(0, 1).unwrap();

    println!("# Swap two columns");
    csv.swap_columns(0, 1).unwrap();

    println!("# Remove line if exist");
    csv.remove_line(0);
    println!("# Remove column column if exist");
    csv.remove_column(0);

    // Save/Write to file

    csv.save("demo.csv").unwrap();
}

终端输出
# Debug

CSV { separator: ';', buffer: [["a", "b", "c", "d"], ["a", "b", "c", "d", "d", "d", "d", "d", "d", "d"]] }

# Out

a;b;c;d;
a;b;c;d;d;d;d;d;d;d;


# Column with name

[0]

# Column content

["a", "a"]

# Line with name

[0, 1]

# Line content

["a", "b", "c", "d"]

# Get at specific location

Some("a")

# Get line count

2

# Get column count

4

# Insert line

# Insert column

# Append line

# Append column

# Correct the size

# Set an element

# Reverse two lines

# Reverse two columns

文件输出
a;insert_column;b;c;d;append_column;;;;;;;
insert_line;SET;;;;append_column;;;;;;;
a;insert_column;b;c;d;d;d;d;d;d;d;append_column;
;append_line;;;;append_column;;;;;;;

无运行时依赖