Editing CSV file with Bangle.js v2 #7193
Replies: 1 comment
-
Posted at 2024-10-24 by ruvi Edit: Removed code that doesn't work. All I've figured out so far is appending a line, and getting the last line:
With Edit: Thinking more about my use case, I don't think it'll ever have to be longer than 50kb, so maybe I can just manipulate everything in RAM. Posted at 2024-10-24 by @gfwilliams You can't really edit a value in the file without re-saving it. Flash memory starts at 1 and can only be made zero, so all you could do with just I think your best bet is to use StorageFile, but if you want to delete a line you just iterate, writing just the stuff you want into a new file: // delete last line
var src = require("Storage").open(STORAGE_FILE_A, "r");
var dst = require("Storage").open(STORAGE_FILE_B, "w");
var line = src.readLine();
var newLine = src.readLine();
while (line && newLine) {
dst.write(line+"\n");
line = newLine;
newLine = src.readLine();
} Since you're loading line by line memory won't be an issue, and for 50k max it shouldn't take too long - it just means you have to have your file name changing (since you can't rename). Or depending how you're working with this, you can just add a line saying Posted at 2024-10-24 by @halemmerich How about having the last line in its own"staging" file? Posted at 2024-10-24 by @gfwilliams
Not if you're constantly writing that staging file? But if you're not leaving the app then just keeping the last value in a variable and being sure to write it in an Posted at 2024-10-24 by @halemmerich Compared to complete rewrites for every corrected line it will save on both. Compared to having a "delete previous line"-marker probably not, depending on data line length. Posted at 2024-10-24 by ruvi Thank you @gfwilliams and @halemmerich for your help. I actually really like the staging file idea. It seems to work well:
I'd only be logging a few dozen times per day, so I don't think flash writes should be a big deal. It's probably not necessary for my project, but it would be cool to have a more general solution for deleting multiple last lines in sequence, which might require something more like what Gordon was saying with rewriting the whole file, or appending the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2024-10-23 by ruvi
I'm working on a project that involves data logging in a CSV, but where there should be the option of editing/deleting the last line of the CSV. I'm not sure how to go about this with a Bangle.
I was thinking maybe something like loading the whole file into a string, deleting the last line, then re-saving it. But I'm worried about the size of the string being larger than the watch can handle if the file gets very long.
Is there a better way to do this?
Edit: Oh, maybe I can somehow access just the last chunk with the
file\5
notation? I don't know if that's a thing.It doesn't necessarily have to be in CSV format.
Beta Was this translation helpful? Give feedback.
All reactions