A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/queryverse/CSVFiles.jl below:

queryverse/CSVFiles.jl: FileIO.jl integration for CSV files

This package provides load and save support for CSV Files under the FileIO.jl package.

Use Pkg.add("CSVFiles") in Julia to install CSVFiles and its dependencies.

To read a CSV file into a DataFrame, use the following julia code:

using CSVFiles, DataFrames

df = DataFrame(load("data.csv"))

To read a gzipped CSV file into a DataFrame:

using CSVFiles, DataFrames

df = DataFrame(load(File(format"CSV", "data.csv.gz")))

The call to load returns a struct that is an IterableTable.jl, so it can be passed to any function that can handle iterable tables, i.e. all the sinks in IterableTable.jl. Here are some examples of materializing a CSV file into data structures that are not a DataFrame:

using CSVFiles, DataTables, IndexedTables, TimeSeries, Temporal, Gadfly

# Load into a DataTable
dt = DataTable(load("data.csv"))

# Load into an IndexedTable
it = IndexedTable(load("data.csv"))

# Load into a TimeArray
ta = TimeArray(load("data.csv"))

# Load into a TS
ts = TS(load("data.csv"))

# Plot directly with Gadfly
plot(load("data.csv"), x=:a, y=:b, Geom.line)

One can load both local files and files that can be downloaded via either http or https. To download from a remote URL, simply pass a URL to the load function instead of just a filename. In addition one can also load data from an IO object, i.e. any stream. The syntax that scenario is

df = DataFrame(load(Stream(format"CSV", io)))

The load function also takes a number of parameters:

load(f::FileIO.File{FileIO.format"CSV"}; <arguments>...)

For example, to load a CSV file that doesn't have the extension ".csv", you need

load(File(format"CSV", "csv_file.txt"))

These are simply the arguments from TextParse.jl, which is used under the hood to read CSV files.

The following code saves any iterable table as a CSV file:

using CSVFiles

save("output.csv", it)

This will work as long as it is any of the types supported as sources in IterableTables.jl.

Compressed CSV files can be created by specifying the .gz file extension:

using CSVFiles

save(File(format"CSV", "output.csv.gz"), df)

One can also save into an arbitrary stream:

using CSVFiles

save(Stream(format"CSV", io), it)

The save function takes a number of arguments:

save(f::FileIO.File{FileIO.format"CSV"}, data; delim=',', quotechar='"', escapechar='"', nastring="NA", header=true)

Both load and save also support the pipe syntax. For example, to load a CSV file into a DataFrame, one can use the following code:

using CSVFiles, DataFrames

df = load("data.csv") |> DataFrame

To save an iterable table, one can use the following form:

using CSVFiles, DataFrames

df = # Aquire a DataFrame somehow

df |> save("output.csv")

The pipe syntax is especially useful when combining it with Query.jl queries, for example one can easily load a CSV file, pipe it into a query, then pipe it to the save function to store the results in a new file.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4