CSV.jl icon indicating copy to clipboard operation
CSV.jl copied to clipboard

Not quoting strings automatically when required

Open TimG1964 opened this issue 1 year ago • 1 comments

See this discussion on Discourse for more details.

I can read a CSV file from an external source that contains the string:

"This award from the Export Development Fund will enhance the export opportunities for British films where they have been selected to appear at important international film festivals.\r"

(note the trailing \r)

CSV.file() reads this field into a DataFrame just fine.

If I subsequently write the DataFrame to another csv file, CSV.write() does not automatically quote the field, and this results in an invalid csv file.

Simple MWE Caution: the file that will be downloaded is over 250MB in size.
using CSV, DataFrames, Dates, HTTP

const url = raw"https://nationallottery.dcms.gov.uk/api/v1/grants/csv-export/"
const dataloc = [data store location]
const n = now()
const outfile = (string(Dates.year(n)) * lpad(string(Dates.month(n)), 2, '0') * lpad(string(Dates.day(n)), 2, '0') * " - DCMS grants.csv")
http_response = HTTP.get(url)

dcms = DataFrame(CSV.File(http_response.body, ntasks=1))


CSV.write(joinpath(dataloc, outfile), dcms)

CSV read will not read this newly written csv file correctly.

Thanks!

TimG1964 avatar Dec 19 '24 13:12 TimG1964

Thanks to @eldee (on Discourse) for this mwe:

using CSV, DataFrames

open("mwe.csv", "w") do file
    write(file, "\"a\r\", 1\n\"b\r\", 2")
end

df = CSV.read("mwe.csv", header=false, DataFrame)
CSV.write("mwe2.csv", df, header=false)
df2 = CSV.read("mwe2.csv", header=false, DataFrame)

TimG1964 avatar Dec 19 '24 14:12 TimG1964