What is the best way to add line breaks when writing text to a CSV file?

Currently, I have product descriptions stored in an object as strings. My goal is to transfer this data to a CSV file for further data analysis.

The challenge lies in making the data visually appealing for easy readability. Thus far, all I've managed to achieve is a collection of tightly packed, hard-to-read strings.

When editing in Excel, using Alt+Enter creates spacing within the same row. How can I write the data to the file with line spaces that don't result in new rows in Excel?

Through my research, I discovered:

  1. \n gets copied as text
  2. \r\n starts a new row instead of simply adding space within the row

For instance:

fs.appendFile(`./data/testCSV.csv`,  encodeURIComponent(element.name +'\r\n'+ element.description + '\r\n'+ element.short_description), function (err) {
 if (err) throw err;
});

This code snippet results in three separate rows of data.

Here's an example of the desired formatting:

https://i.sstatic.net/AVePI.png

I also attempted to find an NPM parser that could solve this issue, but it seems all options create new lines as well.

Answer №1

For multiline fields in a CSV, it is important to enclose the field with quotes:

123,"multi 
line 
text",456

If there are quotes inside the fields, they need to be escaped with another quote:

123,"Samsung 
24"" 
monitor",456

It is uncertain whether Excel will handle this correctly.

Answer №2

Special shoutout to Danny_ds for his contribution.

Here is an optimized version:

 ` "${x} \n ${y} \n\n ${z}" ` 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Embedding a table inside a Bootstrap popover

I'm struggling with adding a table inside a Bootstrap popover. When I click on it, the table doesn't show up. This is my first time trying to insert HTML into a popover, so I don't know the correct way to do it. Any help would be appreciated ...

How come I'm not receiving an error message when I enter an incorrect email or password?

Whenever I try to log in with the wrong password and email, no error message is displayed. Instead, it simply logs me in and takes me to the main page. Can someone please assist me in implementing an error message display for incorrect login details? imp ...

Troubleshooting problem with JS Hashchange Event on Internet Explorer (MSIE

I've been working on loading my WordPress website using AJAX and came across this helpful tutorial. Everything in the tutorial makes sense to me, but it references a plugin called JS Hashchange Event. The problem I encountered is that it utilizes $.br ...

Scope of MongoDB's `.findOne` method

Having trouble with variable scope. var max; ClassModel.findOne({ class: '1a' }, function (err, class1a) { if (err) return handleError(err); max = class1a.members; console.log(max); }); console.log(max); Why does ...

The execution of the Javascript function is not being carried out

Why is alert("Test1") being executed, but alert("Test2") is not running? P.S. I'm currently not utilizing JSON data. <script> $(document).ready(function() { var param = 10; $.getJSON( 'modules/mod_sche ...

Creating form inputs dynamically in HTML and then sending them to a PHP script programmatically

On my webpage, users can click a button to add new form inputs as needed. However, I'm running into an issue trying to access these new inputs in the PHP file where I submit the data. Here is the code snippet for the button within the form on the pag ...

Steps for effectively deleting browsing history on Ionic

While testing my app on Chrome browser, I encountered an issue with clearing old views and cache. This is the process I followed to sign out of my app: https://i.stack.imgur.com/EGgRx.png Upon clicking Log out, the following code is executed: Auth.$s ...

Is there a way to specifically retrieve the number of likes or followers from Facebook, Twitter, Instagram, and Snapchat in order to display them on my HTML website?

Can you provide guidance on how to obtain the total numbers of likes or followers for our Facebook, Twitter, Instagram, and Snapchat pages without including the plugin boxes or buttons? We are solely interested in the actual numbers. Would this be feasibl ...

The fading effects are not functioning as expected in the following code

Hey there, I'm not the most tech-savvy person, but I managed to come up with this code for page redirection. Unfortunately, I couldn't quite get the fade out and fade in effects to work properly when executing it. If anyone out there can help me ...

Error with ngModel on radio inputs within an ngRepeat

Plunk: http://plnkr.co/edit/Y4ntAj2fIIpexKjkCvlD?p=preview In an attempt to develop a form component that allows users to create multiple instances of a specific data field, I am facing an issue where the radio buttons are not updating the selected item a ...

Sorting list items using jQuery works seamlessly in modern browsers and IE7, however, it encounters issues specifically in IE9

Seems like there might be an issue here. It's not functioning properly in IE9. Could it be that IE9 doesn't handle JavaScript as expected? It's working fine in Chrome, FF, Safari, and even in IE7 (I verified this myself). If you want to tak ...

Acquire the nested object within a MongoDB document using its assigned ID

Looking to retrieve and edit a specific comment within a post, but not sure where to start. Here is an example of my Post data structure: { "title" : "First Node.js App", "body" : "testing 123", "st ...

Retrieve the most recent information based on the ID from an object by utilizing timestamps within Angular 6

I have an array of objects stored in the 'component' variable component=[{id:1,date:'20-10-2020'},{id:1,date:'13-01-2020'},{id:2,date:'30-03-2020'}] Within this array, there are 2 objects with the same 'id&apos ...

Is there a way to retrieve the properties of another function within the same component?

I am trying to place NumberFormat inside OutlinedInput and I also need different properties for the format of NumberFormat. (There will be a select window that defines which format property to use). This is what I have: import OutlinedInput from "@ma ...

useEffect does not trigger a rerender on the primary parent component

I am facing an issue where the main parent component does not re-render when I change the state 'click button' in another component while using useEffect. Oddly enough, the main <App /> component only re-renders properly when I reload the p ...

"Enhance your Django website with a dynamic voting button using AJAX

I am working on a project where I have implemented the following code: (r'^oyla/(\d+)/$', oyla), Here is the view associated with this code snippet: @login_required def oyla(request, id): if request.is_ajax(): entry = Entry.ob ...

One easy method to verify a visitor's Facebook login status

Currently, I am in the process of developing a WordPress plugin and looking for an easy way to determine if a page visitor is logged in with Facebook. Upon attempting to use the JavaScript SDK, I encountered the following error: The given URL is not all ...

Adjust mouse coordinates to be relative to the coordinates of the <canvas> element

I'm currently facing the challenge of determining the exact location of the mouse on a canvas grid while still maintaining resizability. As of now, I have the mouse coordinates based on its position on the screen (x and y). The issue arises from the ...

Steps for showcasing an image while performing in-memory processing with AngularJS

As a newcomer to JS, I am seeking guidance on how to enhance my AngularJS app. The app features a page that stores data in-memory and allows users to filter this data, which is then displayed in an ng-grid table. Is there a way to show an image like a spin ...

Encountering a Caution Message When Attempting to Install Yeomen

Upgrade to a higher version is recommended at [email protected], but how should I proceed? Please provide guidance on achieving this upgrade. C:\Program Files>npm install -g yo npm WARN deprecated <a href="/cdn-cgi/l/email-protection" clas ...