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
Answer from John Machin on Stack Overflow
Top answer
1 of 16
75

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
2 of 16
33

After lots of tweaking, here's a configuration that works generating files on Linux, reading on Windows+Excel, though the embedded newline format is not according to the standard:

  • Newlines within a field need to be \n (and obviously quoted in double quotes)
  • End of record: \r\n
  • Make sure that you don't start a field with equals, otherwise it gets treated as a formula and truncated

In Perl, I used Text::CSV to do this as follows:

use Text::CSV;

open my $FO, ">:encoding(utf8)", $filename or die "Cannot create $filename: csv = Text::CSV->new({ binary => 1, eol => "\r\n" });

#for each row...:
$csv -> print ($FO, \@row);
🌐
Easydatatransform
easydatatransform.com › help › latest › windows › html › csv_format.html
Reference > File formats > CSV format
CSV is a type of delimited text file format. The column delimiter is usually a Comma, but not always. The row delimiter is Line Feed, Carriage Return or Carriage Return+Line Feed.
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 741859 › column-delimitter-is-shifting-to-new-row-on-new-li
column delimitter is shifting to new row on new line in quoted text value - Microsoft Q&A
Hence you are seeing your data ... me, it seems your source file data itself is not in proper format. There is not clear delimiter to define new row in file....
🌐
Medium
lovespreadsheets.medium.com › what-are-csv-files-and-how-do-i-deal-with-them-825162ec7573
What are CSV files? And how do I deal with them? | by Love Spreadsheets | Medium
September 22, 2022 - A CSV file stores data in rows and the values in each row is separated with a separator, also known as a delimiter.
🌐
MrExcel
mrexcel.com › forums › question forums › power tools
Line break causing issues combining .csv file with comma delimiter | MrExcel Message Board
October 13, 2023 - I'll frequently use power query to get and combine multiple .csv files in a sharepoint folder, this makes it easy for me to update the data set daily. Recently I encountered an error where some data was entering as a new row when it should have been part of the previous row and was therefore ending up with data in the wrong columns etc.
🌐
SAP Community
answers.sap.com › questions › 9802996 › row-delimiter-in-csv-file.html
Solved: Row Delimiter in .csv file - SAP Community
May 4, 2019 - This would add x'0D0A' line break to your rows of data. ... This will help u .... ... thanks for your answer......I have already searched in that but couldn't get answer for my question. ... First of all, comma-separated values are a problem... you would want to wrap your alpha fields in double-quote characters, like "data, data" to avoid incorrect processing in the program reading the .CSV file.
🌐
OpenText
docs.microfocus.com › UCMDB › 11.0 › cp-docs › docs › eng › doc_lib › Content › ides_c_csv_files.htm
Comma Separated Value (CSV) Files - Micro Focus
Values in a single column represent the same type of data. Therefore a CSV file represents a table of data (with rows and columns). The default delimiter for CSV files is the comma, but any symbol can be used as a CSV delimiter, for example, a horizontal tab.
🌐
Stack Overflow
stackoverflow.com › questions › 29713095 › how-do-i-create-a-new-row-delimiter-when-trying-to-convert-a-long-chain-to-a-c
How do I create a "new row" delimiter when trying to convert a long chain to a CSV in R? - Stack Overflow
xyz <- "New York City,1960,1988,Los Angeles,1962,1975,1995,Chicago,1975,2005" library(stringi) stri_split_fixed( strsplit( gsub(",([A-Z])", "crazyRowDelimiter\\1", xyz), ## Make a row delimiter "crazyRowDelimiter", TRUE)[[1]], ## Split on that delimiter ",", simplify = TRUE) ## Split again on commas # [,1] [,2] [,3] [,4] # [1,] "New York City" "1960" "1988" "" # [2,] "Los Angeles" "1962" "1975" "1995" # [3,] "Chicago" "1975" "2005" ""
Find elsewhere
🌐
Ablebits
ablebits.com › ablebits blog › excel › csv › how to change excel csv delimiter to comma or semicolon
How to change Excel CSV delimiter to comma or semicolon
March 9, 2023 - For Excel to be able to read a CSV file with a field separator used in a given CSV file, you can specify the separator directly in that file. For this, open your file in any text editor, say Notepad, and type the below string before any other data: ... In a similar fashion, you can use any other character for the delimiter - just type the character after the equality sign.
🌐
Python
docs.python.org › 3 › library › csv.html
csv — CSV File Reading and Writing
for row in spamreader: ... print(', '.join(row)) Spam, Spam, Spam, Spam, Spam, Baked Beans Spam, Lovely Spam, Wonderful Spam ... Return a writer object responsible for converting the user’s data into delimited strings on the given file-like object. csvfile can be any object with a write() method. If csvfile is a file object, it should be opened with newline='' [1]. An optional dialect parameter can be given which is used to define a set of parameters specific to a particular CSV dialect.
🌐
Alteryx Community
community.alteryx.com › t5 › Alteryx-Designer-Desktop-Discussions › Read-CSV-file-with-a-specific-newline-delimiter › td-p › 821310
Solved: Read CSV file with a specific newline delimiter - Alteryx Community
September 16, 2021 - Mark as New · Bookmark · Subscribe · Mute · Subscribe to RSS Feed · Permalink · Print · Notify Moderator · Hi, I apologize for my "noob" question. I have a csv file which I download from a URL with the following pattern : periode;ID;nbPrev<br/>22;34;4323<br/>22;44;984<br/>22;6;389<br/> etc... And I want to read it as a 3 column table, with the classic seperator ";". But how can I interpret the delimiter "<br/>" as a newline delimiter to read the file correctly ?
🌐
Online Tools
onlinetools.com › csv › change-csv-delimiter
Change CSV Delimiter – Online CSV Tools
Free online CSV column delimiter changer. Just upload your CSV file in the form below and it will automatically get a new column delimiter character. In the tool options, you can specify which delimiter and quote characters are used in the source ...
Top answer
1 of 2
1
  • Open a new blank workbook.
  • Click the Sheet1 name tab and choose View Code.
  • When the VBE opens, paste the following into the code sheet titled Sheet1 (Code).

  Code:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Parent.UsedRange.Cells.Count = 1 And _
       Target.Cells.Count = 1 And _
       Target.Address(0, 0) = "A1" Then
        On Error GoTo safe_exit
        Application.EnableEvents = False
        Dim a As Long, b As Long, arr1 As Variant, arr2 As Variant, hdr As Variant, tmp As Variant
        arr1 = Split(Replace(Target.Value2, ",Name:", vbLf & "Name:"), vbLf)
        For a = LBound(arr1) To UBound(arr1)
            arr2 = Split(arr1(a), Chr(44))
            For b = LBound(arr2) To UBound(arr2)
                tmp = Split(arr2(b), Chr(58))
                hdr = Application.Match(tmp(0), Rows(1), 0)
                If IsError(hdr) Then
                    Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1) = tmp(0)
                    hdr = Application.Match(tmp(0), Rows(1), 0)
                End If
                Cells(2 + a, hdr) = tmp(1)
            Next b
        Next a
        Columns(1).EntireColumn.Delete
    End If

safe_exit:
    Application.EnableEvents = True

End Sub

   - Tap Ctrl+Q to return to your new, blank worksheet. - Open the TXT file in notepad or some other text editor and copy the whole thing. - Go back to your new, blank Sheet1 and paste the text into A1.

2 of 2
1

As I said in my comments, you can replace ,Name: with \nName: (new line/carriage return).

This is very easy to do in Notepad++ (a free program)

https://notepad-plus-plus.org/

Make sure you set the Search Mode to Extended

Use Find: ,Name:

Replace With: \nName:

It comes over looking like this when you set it to Delimited using Comma

🌐
KNIME Community
forum.knime.com › knime analytics platform
carriage return as Row Delimiter in CSV Reader - KNIME Analytics Platform - KNIME Forum Archive
September 3, 2013 - I am trying to load a csv file with carriage return line endings using CSV Reader. (Unfortunately, Mac Excel exports csv files using carriage returns. I know I can convert line endings, but our users would prefer using the files directly from Excel.) I thought that changing Row Delimiter from the default \n to \r would work, but it doesn't. I get an empty table, with all cells as column headers.
🌐
Affinity
support.affinity.co › hc › en-us › articles › 360044453711-How-to-open-CSV-files-with-the-correct-delimiter-separator
How to open CSV files with the correct delimiter/separator – Affinity Help Center
March 27, 2023 - ... Skip a line at the top, and add sep=; if the separator used in the CSV is a semicolon (;), or sep=, if the separator is a comma (,). Save, and re-open the file. Note: This newly added line will not show up when opening the file in Excel.