This repository has been archived by the owner on Jun 27, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testm.rs
55 lines (44 loc) · 1.45 KB
/
testm.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use csv::ReaderBuilder;
use serde::{Serialize, Deserialize};
use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::fs::OpenOptions;
use std::io::Write;
#[derive(Serialize, Deserialize)]
struct AvailableAgencies {
agencies: Vec<String>,
}
fn main() -> Result<(), Box<dyn Error>> {
// Open the CSV file
let file = File::open("urls.csv")?;
let reader = BufReader::new(file);
// Create a CSV reader
let mut csv_reader = ReaderBuilder::new().flexible(true).from_reader(reader);
// Create a vector to hold the available agencies
let mut available_agencies = AvailableAgencies {
agencies: Vec::new(),
};
// Iterate over each record (line) in the CSV file
for result in csv_reader.records() {
// Unwrap the record
let record = result?;
// Check the number of fields in the record
if record.len() > 0 {
// Get the first field of the record
let agency = record.get(0).unwrap().to_owned();
// Store the agency in the vector
available_agencies.agencies.push(agency);
}
}
// Serialize the available agencies to JSON
let json_data = serde_json::to_string(&available_agencies)?;
// Write the JSON data to a file
let mut file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open("available_agencies.json")?;
file.write_all(json_data.as_bytes())?;
Ok(())
}