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

Not quoting strings automatically when required #1153

Open
TimG1964 opened this issue Dec 19, 2024 · 1 comment
Open

Not quoting strings automatically when required #1153

TimG1964 opened this issue Dec 19, 2024 · 1 comment

Comments

@TimG1964
Copy link

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
Copy link
Author

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)

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

No branches or pull requests

1 participant