[p5.js 2.0 Bug Report]: saveTable() doesn't escape double quotes in CSV, round-trip corrupts data
Most appropriate sub-area of p5.js?
- [ ] Accessibility
- [ ] Color
- [ ] Core/Environment/Rendering
- [ ] Data
- [ ] DOM
- [ ] Events
- [ ] Image
- [x] IO
- [ ] Math
- [ ] Typography
- [ ] Utilities
- [ ] WebGL
- [ ] Build process
- [ ] Unit testing
- [ ] Internationalization
- [ ] Friendly errors
- [ ] Other (specify if possible)
p5.js version
2.x (main)
Web browser and version
any
Operating system
any
Steps to reproduce this
saveTable() wraps values containing commas in quotes (that got fixed in an old PR), but it never escapes " characters inside those values. So any cell with both a comma and a quote produces malformed CSV.
function setup() {
let t = new p5.Table();
t.addColumn('name');
t.addColumn('note');
let r = t.addRow();
r.setString('name', 'thing');
r.setString('note', 'He said "yes, go ahead"');
saveTable(t, 'out.csv');
}
Output:
name,note
thing,"He said "yes, go ahead""
That middle " is unescaped. Most CSV parsers (Excel, Python csv, loadTable itself) will choke on it or misread the row.
RFC 4180 says inner quotes must be doubled. Should be:
name,note
thing,"He said ""yes, go ahead"""
Also: headers with commas or quotes are never escaped either. And a newline inside a value will silently break row boundaries with no quoting at all.
loadTable handles all of this correctly on the way in. So load a valid CSV → save it back → file is now broken. Classic round-trip corruption.
The fix is one line per write call: escape " as "" before wrapping, and trigger quoting on " or \n too, not just ,.
@ksen0 @davepagurek what do you think of this?? ready to go, just let me know
Hi @apoorvdarshan , I really appreciate the enthusiasm, but in the future, please follow the contributor guidelines, and wait for approval and assignment before beginning work / filing PRs. There are several higher-priority tasks right now, so even though you made a PR, it may take some time to get to it. To avoid delays and duplicate work is one of the reasons behind the guidelines. Thanks for understanding!
EDIT: So sorry, tagged the wrong author. I meant to tag the PR author. Apologies!!
Hey @ksen0 just to clarify, I haven't worked on this yet or raised a Pr, someone else picked it up. I raised this issue and was waiting for your heads up as always.
Same goes for #8552
Hey @ksen0 @avinxshKD — apologies for jumping ahead here. I should have followed the contributing guidelines and waited for assignment before putting up the PR. Closing #8553 now. Sorry for the duplicate effort!
So sorry @avinxshKD for tagging you wrongly! Thanks for the patience. I edited my comment above as well. Thanks for understanding @apoorvdarshan
@ksen0 hey no worries at all, working on #8552 as asked, thanks
hey @ksen0 as #8552 got merged, so is this a good time to work on this or #8540 now??
Hi @avinxshKD thanks for the patience and the reminder yeah looks like this is free, assigned!
@ksen0 on it
Hi @avinxshKD are you still wanting to work on this? the other PR was closed as it was target main, but if you target 2.0 this is open for work.
@ksen0 Sure sure, I will do that. Thanks for reminding :).
@ksen0 hey I am not able to see 2.0 branch, do you mean V1 branch by any chance??
@avinxshKD we recently moved p5 2.0 development to the main branch now. So if you update your fork of p5, main will have the p5 2.0 code.