Writing into .csv file with write_field

Try to write a variable of type ethereum_types::H256 to a .csv file. When I did it, it was not writing anything to the file.

I tried to change it into String , with to_string() , then something was written to the file, but not what I expected. I tried using write_record() but it does also not write anyting to the file.

use std::error::Error;
use csv::Writer;
use std::fs::OpenOptions;
use ethereum_types::H256;

fn example(data: H256) -> Result<(), Box<dyn Error>> {
    let file = OpenOptions::new()
        .write(true)
        .create(true)
        .append(true)
        .open("/home/shashi/test.csv")
        .unwrap();
    let mut wtr = Writer::from_writer(file);
    wtr.write_field(data)?;
    wtr.write_record(None::<&[u8]>)?;
    Ok(())
}

fn main()
{   
    let s= H256::zero();
    for n in  1..5
    {
        if let Err(err) = example(s)
        {
          println!("{}", err);
        }
    }
}

This question already has an answer here:

Try to write a variable of type ethereum_types::H256 to a .csv file. When I did it, it was not writing anything to the file.

I tried to change it into String , with to_string() , then something was written to the file, but not what I expected. I tried using write_record() but it does also not write anyting to the file.

use std::error::Error;
use csv::Writer;
use std::fs::OpenOptions;
use ethereum_types::H256;

fn example(data: H256) -> Result<(), Box<dyn Error>> {
    let file = OpenOptions::new()
        .write(true)
        .create(true)
        .append(true)
        .open("/home/shashi/test.csv")
        .unwrap();
    let mut wtr = Writer::from_writer(file);
    wtr.write_field(data)?;
    wtr.write_record(None::<&[u8]>)?;
    Ok(())
}

fn main()
{   
    let s= H256::zero();
    for n in  1..5
    {
        if let Err(err) = example(s)
        {
          println!("{}", err);
        }
    }
}

I epected

"0x0000000000000000000000000000000000000000000000000000000000000000"

but it was

"0x0000...0000"

Found the solution:

wtr.serialize(data)?;

will work fine.

Your first example works just fine and as expected. It writes a bunch of NUL bytes to test.csv. If you cat this file, it might look blank, but the NUL bytes are there. Presumably, this is the raw representation of H256.

The serialize approach works because it appears that the Serialize implementation on H256 converts the internal representation to a string form: 0x0000000000000000000000000000000000000000000000000000000000000000.

When you use to_string(), it converts its internal representation to a condensed human readable form, which is how you get 0x0000…0000. (This is probably not quite right---it might be better as a Debug impl instead.) And so that's what csv writes.

TL;DR - You problem doesn't have anything to do with CSV. This is just a general encoding problem, and you've got to pick how to represent your types.

Is there any way I can append/concat H256 to a String ? I can't change H256 to a String because it change the format.

You'd have to get the raw bytes and then format it to a string yourself. Personally, this seems like an oversight in the API of ethereum-types, and might warrant a bug report.