Copy link to clipboard
Copied
Hello.
Need to edit a csv as shown below, but only one line, for example B,
| TEXT | NUMBER |
|---|---|
| A | 1 |
| B | 2 |
| C | 3 |
Where the new one is as shown below.
| TEXT | NUMBER |
|---|---|
| A | 1 |
| F | 5 |
| C | 3 |
I know only add values in csv and overwrite in this case need to edit something in the middle of the csv.
Can anybody help me?.
Thank you very much.
Copy link to clipboard
Copied
Ok, so I don't know how to re-write to a CSV file, but assuming you know for which row you want to change the data, you can split the theCSV by line breaks, then split the desired row by comma, then change the values via their index.
The below script gets that far, but as i said, i don't know how to write the changed information back to the CSV file, so I'd love for someone to chime in on that part as that is definitely information that could be very useful to me.
function wrapper()
{
var theCSV = File("~/Desktop/testCSV.csv");
theCSV.open();
var contents = theCSV.read();
theCSV.close();
var lines = contents.split("\n");
var edit = lines[0].split(",");
alert("edit = \n" + edit[0] + "\n" + edit[1]);
edit[0] = "F";
edit[1] = "5";
alert("edit = \n" + edit[0] + "\n" + edit[1]);
}
wrapper();
Copy link to clipboard
Copied
Thank you Willian, unfortunately I have tested this method, but my difficulty is just rewriting the csv without having to use a loop that re-write all the csv with the changed line.
Copy link to clipboard
Copied
I think there's no other way than to read the entire file and write it back with the one edit. Luckily, the computer can do this at lightning-fast speed!
To write a file:
var contents = "Hello, World!";
var myFilePath = "path/string/here/file.csv";
var newFile = File(myFilePath);
newFile.open('w'); // 'w' is for 'write'
newFile.write(contents);
newFile.close();
Copy link to clipboard
Copied
so what's the best way to implement this then if you're writing an array back to the CSV instead of a simple string?
Can you simply write the array to the CSV like:
var contents = [["Something", "Something Else",],["Blah", "BlahBlah"]];
newFile.open('w');
newFile.write(contents);
newFile.close();
I can't imagine that would work. So i'm guessing one would have to write a nested loop to write the info from the nested arrays into the CSV file right?
var contents = [["Something", "Something Else",],["Blah", "BlahBlah"]];
newFile.open('w');
for (var a=0;a<contents.length;a++)
{
var curRow = contents;
for (var b=0;b<curRow.length;b++)
{
newFile.write(curRow + ","
}
newFile.write("\n"); // that wouldn't work would it... How would one add a line break proper?
}
newFile.close();
Copy link to clipboard
Copied
I would not do file writing until I had the whole string strang together.
I think the way you have your loop is the right track, and the nextline would go where you put it.
You can also take advantage of the nice array-to-string shortcuts in javascript.
var contents = [["Something", "Something Else",],["Blah", "BlahBlah"]];
var str = contents.join("\n");
alert(str);
The join function turns the whole thing to string:
Something,Something Else
Blah,BlahBlah
After this is written to a text file, it can be opened up and will have the items in different cells in a spreadsheet program.
But, if you have a comma inside one of your cells as part of the text, it complicates things so you will have to follow whatever the standard is of your spreadsheet program (Excel keeps cells wrapped in quotes together) to keep your cells together.
var contents = [["Something", "Something Else",],["Blah", "\"Blah,Blah\""]];
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more