0

我正在尝试重新学习 rust 中的数据科学。

我有一个Vec<String>包含分隔符“|” 和一个新行“!结束”。

我想结束的是Vec<Vec<String>>可以放入 2D ND 阵列。

我有这个python代码:

file = open('somefile.dat')
lst = []
for line in file:
    lst += [line.split('|')]
    
df = pd.DataFrame(lst)
SAMV2FinalDataFrame = pd.DataFrame(lst,columns=column_names)

我在这里用 rust 重新创建了它:



fn lines_from_file(filename: impl AsRef<Path>) -> Vec<String> {
    let file = File::open(filename).expect("no such file");
    let buf = BufReader::new(file);
    buf.lines()
        .map(|l| l.expect("Could not parse line"))
        .collect()
}

fn main() {
    let lines = lines_from_file(".dat");
    let mut new_arr = vec![];
//Here i get a lines immitable borrow
    for line in lines{
        new_arr.push([*line.split("!end")]);
    }

// here i get expeected closure found str
let x = lines.split("!end");



let array = Array::from(lines)

我有什么:['1','1','1','end!','2','2','2','!end'] 我需要什么:[['1',' 1','1'],['2','2','2']]

编辑:还有为什么当我涡轮鱼时它会在 Stack Overflow 上消失?

4

1 回答 1

1

我认为您遇到的部分问题是由于您使用数组的方式。例如,Vec::push只会添加一个元素,因此您希望使用它Vec::extend。我还遇到了一些空字符串的情况,因为拆分"!end"会在子字符串的末端留下尾随'|'。错误很奇怪,我不完全确定关闭的来源。

let lines = vec!["1|1|1|!end|2|2|2|!end".to_string()];
let mut new_arr = Vec::new();

// Iterate over &lines so we don't consume lines and it can be used again later
for line in &lines {
    new_arr.extend(line.split("!end")
        // Remove trailing empty string
        .filter(|x| !x.is_empty())
        // Convert each &str into a Vec<String>
        .map(|x| {
            x.split('|')
                // Remove empty strings from ends split (Ex split: "|2|2|2|")
                .filter(|x| !x.is_empty())
                // Convert &str into owned String
                .map(|x| x.to_string())
                // Turn iterator into Vec<String>
                .collect::<Vec<_>>()
    }));
}

println!("{:?}", new_arr);

我还提出了另一个版本,它应该可以更好地处理您的用例。较早的方法丢弃了所有空字符串,而这种方法应该在正确处理"!end".

use std::io::{self, BufRead, BufReader, Read, Cursor};

fn split_data<R: Read>(buffer: &mut R) -> io::Result<Vec<Vec<String>>> {
    let mut sections = Vec::new();
    let mut current_section = Vec::new();
    
    for line in BufReader::new(buffer).lines() {
        for item in line?.split('|') {
            if item != "!end" {
                current_section.push(item.to_string());
            } else {
                sections.push(current_section);
                current_section = Vec::new();
            }
        }
    }
        
    Ok(sections)
}

在此示例中,我用于Read更轻松的测试,但它也适用于文件。

let sample_input = b"1|1|1|!end|2|2|2|!end";
println!("{:?}", split_data(&mut Cursor::new(sample_input)));
// Output: Ok([["1", "1", "1"], ["2", "2", "2"]])

// You can also use a file instead
let mut file = File::new("somefile.dat");
let solution: Vec<Vec<String>> = split_data(&mut file).unwrap();

游乐场链接

于 2022-02-18T20:24:13.173 回答