Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add quote_style option to csv IO functions #1049

Merged
merged 7 commits into from
Jan 10, 2025

Conversation

jdbarillas
Copy link
Contributor

@jdbarillas jdbarillas commented Jan 8, 2025

Implements the QuoteStyle options for CsvWriter into to_csv and dump_csv (with :necessary as default)

New behavior:

iex(2)> df = Explorer.Datasets.fossil_fuels() |> Explorer.DataFrame.head(2)
iex(3)> dump_csv(df)                                                       
{:ok,
 "year,country,total,solid_fuel,liquid_fuel,gas_fuel,cement,gas_flaring,per_capita,bunker_fuels\n2010,AFGHANISTAN,2308,627,1601,74,5,0,0.08,9\n2010,ALBANIA,1254,117,953,7,177,0,0.43,7\n"}
iex(4)> dump_csv(df, [quote_style: :always])                               
{:ok,
 "\"year\",\"country\",\"total\",\"solid_fuel\",\"liquid_fuel\",\"gas_fuel\",\"cement\",\"gas_flaring\",\"per_capita\",\"bunker_fuels\"\n\"2010\",\"AFGHANISTAN\",\"2308\",\"627\",\"1601\",\"74\",\"5\",\"0\",\"0.08\",\"9\"\n\"2010\",\"ALBANIA\",\"1254\",\"117\",\"953\",\"7\",\"177\",\"0\",\"0.43\",\"7\"\n"}

iex(5)> dump_csv(df, [quote_style: :non_numeric])                          
{:ok,
 "\"year\",\"country\",\"total\",\"solid_fuel\",\"liquid_fuel\",\"gas_fuel\",\"cement\",\"gas_flaring\",\"per_capita\",\"bunker_fuels\"\n2010,\"AFGHANISTAN\",2308,627,1601,74,5,0,0.08,9\n2010,\"ALBANIA\",1254,117,953,7,177,0,0.43,7\n"}

TO DO:

  • Tests

This is my first experience with Rust so any feedback would be appreciated.

@billylanchantin
Copy link
Contributor

Thanks, this seems useful!

Rust code looks good at a glance. And yes we'll need a test or two (on the Elixir side).

@jdbarillas
Copy link
Contributor Author

Tests added

Keyword.validate!(opts,
header: true,
delimiter: ",",
quote_style: :necessary,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need documentation on the supported values for this function and we are good to go :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, let me know if there's any additional places needed

@philss philss merged commit d2dfe83 into elixir-explorer:main Jan 10, 2025
4 checks passed
@philss
Copy link
Member

philss commented Jan 10, 2025

@jdbarillas thank you! :D

@jdbarillas jdbarillas deleted the add-quotestyle-to-csv-io branch January 10, 2025 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants