How can I convert a .txt file to .csv format without using Excel?

44,346

Assuming the text is properly escaped (i.e. embedded commas and linefeeds are double-quoted) you can just rename the file to .csv. There is nothing otherwise special about a CSV file format. It is essentially a text file.

There's actually a spec for CSV (you can read here). Note the rules for enclosing fields with double quotes: "Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes," and "If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote."

For example:

"You want to convert text to CSV," she noted.
This line doesn't need quotes (but they don't hurt).

Becomes:

"""You want to convert text to CSV,"" she noted."
"This line doesn't need quotes (but they don't hurt)."
Share:
44,346

Related videos on Youtube

user216357
Author by

user216357

Updated on September 18, 2022

Comments

  • user216357
    user216357 over 1 year

    I have a .txt file consisting of very long paragraphs, with a single newline character separating each paragraph. I would like to convert this to a .csv format so that the file effectively has one column holding all the paragrahs, i.e., the first cell in each "row" contains the entire paragraph. However, I cannot use Excel to do this by simply opening the file in Excel and saving it as .csv, because some of my paragraphs are larger than the 1015 character limit that MS Excel has on a single field.

  • user216357
    user216357 about 11 years
    It's not escaped. I guess I could do a find+replace for all the relevant characters to add the double quotes, though. Thanks.
  • Bradd Szonye
    Bradd Szonye about 11 years
    To turn the current text into valid CSV, you can replace every double quote with a pair of double quotes, and then add a double quote to the beginning and end of each line.
  • Jamie Hanrahan
    Jamie Hanrahan almost 7 years
    Yes. Notepad++ would be especially good for this.