Printing a New Line in a CSV File Cell

printing a new line in a csv file cell

Try this

"\n"

inside double quote

Adding a newline character within a cell (CSV)

This question was answered well at Can you encode CR/LF in into CSV files?.

Consider also reverse engineering multiple lines in Excel. To embed a newline in an Excel cell, press Alt+Enter. Then save the file as a .csv. You'll see that the double-quotes start on one line and each new line in the file is considered an embedded newline in the cell.

Generating CSV file for Excel, how to have a newline inside a value

You should have space characters at the start of fields ONLY where the space characters are part of the data. Excel will not strip off leading spaces. You will get unwanted spaces in your headings and data fields. Worse, the " that should be "protecting" that line-break in the third column will be ignored because it is not at the start of the field.

If you have non-ASCII characters (encoded in UTF-8) in the file, you should have a UTF-8 BOM (3 bytes, hex EF BB BF) at the start of the file. Otherwise Excel will interpret the data according to your locale's default encoding (e.g. cp1252) instead of utf-8, and your non-ASCII characters will be trashed.

Following comments apply to Excel 2003, 2007 and 2013; not tested on Excel 2000

If you open the file by double-clicking on its name in Windows Explorer, everything works OK.

If you open it from within Excel, the results vary:

  1. You have only ASCII characters in the file (and no BOM): works.
  2. You have non-ASCII characters (encoded in UTF-8) in the file, with a UTF-8 BOM at the start: it recognises that your data is encoded in UTF-8 but it ignores the csv extension and drops you into the Text Import not-a-Wizard, unfortunately with the result that you get the line-break problem.

Options include:

  1. Train the users not to open the files from within Excel :-(
  2. Consider writing an XLS file directly ... there are packages/libraries available for doing that in Python/Perl/PHP/.NET/etc

Reading csv files with new lines

This method will print in the format you requested and number your rows as well.

import pandas as pd

data = pd.read_csv('test.csv', header = None, names = ['Team Name', 'Number', 'Score'])

print(data)

Output:

      Team Name  Number  Score
0 Team One 23 NaN
1 Team Two 102 NaN
2 Team Three 44 NaN
3 Team Four 40 NaN

Importing CSV with line breaks in Excel 2007

I have finally found the problem!

It turns out that we were writing the file using Unicode encoding, rather than ASCII or UTF-8. Changing the encoding on the FileStream seems to solve the problem.

Thanks everyone for all your suggestions!

csv file output new line

To write in each cell, example would be

import csv

my_data = "Example"

with open("FB_available_account.csv", "w") as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerow([my_data])

Programmatically move to a new line within a cell in .csv file through an Android application in Java

You need to understand the difference between

"\n" + "\"" +  flightnumber  + "\""

and

"\n" + "\"" + "flightnumber" + "\""
"\n" + "\"flightnumber\""
"\n\"flightnumber\""

For better readability, and since StringBuilder is already used to concatenate strings, here's everything on a separate line:

data.append("Flight Number");
data.append(',');
data.append("Departure Date");
data.append(',');
data.append("Comments");
data.append('\n');
// ...
data.append(flightNumber);
data.append(',');
data.append(departureDate);
data.append(',');
data.append('"' + pre_comments_consolidated + '"');

Notes:

  • Put single characters in single quotes : '\n', 'a', 'b', 'c'
  • You don't have to escape double quotes inside single quotes and vice versa: "'", '"'
  • You can concatenate characters and strings: "example" + '\n'
  • You're joining literal double quotes with variable pre_comments_consolidated.

This will not work properly if the CSV value contains quotes. You'll need to CSV-escape them. You should consider putting all that logic in a separate function.

private String escapeCsv(String in) {
// Double double quotes that are already part of the value.
String out = in.replace("\"", "\"\"");
// Wrap the whole thing in double quotes.
return '"' + out '"'
}

Use like this:

data.append(escapeCsv(pre_comments_consolidated));

Be mindful of

  • which quotes you need to denote a Java string,
  • which quotes you need printed literally,
  • which quotes need escaping (either in Java or in a format down the line, like CSV).

Write newline to csv in Python

change 'w' to 'a'

with open('outfile.csv','a')


Related Topics



Leave a reply



Submit