TextSorter

How to Convert a List to Comma Separated Values (Lines to CSV)

· 7 min read

You have a list of items. One per line. And you need them on a single line, separated by commas.

That’s it. That’s the whole problem. And yet somehow people spend way too long doing this manually, especially when the list has 200+ items.

The Problem Everyone Has

Here’s what happens practically every day in offices around the world. Someone has a list like this:

apple
banana
cherry
date
elderberry

And they need it to look like this:

apple, banana, cherry, date, elderberry

Maybe it’s for an SQL query. Maybe it’s for a CSV file. Maybe it’s for a form field that accepts comma-separated tags. Maybe it’s because their boss asked for “everything on one line” in an email.

Whatever the reason, manually typing commas between 10 items is annoying. Between 100 items? Not happening.

The 3-Second Fix

Paste your list into TextSorter’s Text to CSV tool. Pick your delimiter. Copy the result. Go do something more interesting with your time.

The tool handles the boring parts: trimming whitespace from each item, skipping empty lines, and joining everything with your chosen separator. It runs in your browser, so if your list contains customer IDs, email addresses, or anything sensitive, none of it leaves your device.

Now, if you want to understand the mechanics and do it in Excel, Sheets, or code, keep reading.

Doing It in Excel

Excel has a few approaches depending on your version.

TEXTJOIN Function (Excel 365 / 2019+)

If you have a modern version of Excel, this is the cleanest method:

=TEXTJOIN(",", TRUE, A1:A100)

That formula joins everything in cells A1 through A100 with commas. The TRUE argument tells it to skip empty cells, which is almost always what you want.

Want spaces after the commas for readability? Change the delimiter:

=TEXTJOIN(", ", TRUE, A1:A100)

Want each value wrapped in quotes for SQL? It gets a bit more complex:

=TEXTJOIN(",", TRUE, "'" & A1:A100 & "'")

This wraps each value in single quotes and separates them with commas. Perfect for building SQL IN clauses.

One thing to know: TEXTJOIN has a character limit of 32,767 characters in the result. If your combined list exceeds that, you’ll get an error. For most use cases this is plenty, but if you’re joining thousands of long strings, you’ll need a different approach.

CONCATENATE or The Ampersand Trick (Older Excel)

If you’re on an older Excel version without TEXTJOIN:

=A1 & "," & A2 & "," & A3

Yeah, this doesn’t scale. For long lists, you’d need a helper column or VBA. Honestly, at that point, paste the column into the Text to CSV tool and save yourself the formula headache.

The Transpose-Paste Trick

Copy your column, then use Paste Special > Transpose to paste it into a row. Then manually add commas between the cells… which is still tedious. The TEXTJOIN formula is better.

Doing It in Google Sheets

Google Sheets has had JOIN and TEXTJOIN for years:

=JOIN(",", A1:A100)

Or with the more feature-rich TEXTJOIN:

=TEXTJOIN(",", TRUE, A1:A100)

Same behavior as Excel. Skip blanks, join with commas.

Google Sheets also supports array formulas that can add formatting on the fly:

=JOIN(",", ARRAYFORMULA("'" & A1:A10 & "'"))

This wraps each value in quotes. Handy for generating SQL or JSON arrays.

Doing It in Code

This is one of those operations that every programming language handles slightly differently.

JavaScript

const list = `apple
banana
cherry
date`;

const csv = list.split('\n').map(s => s.trim()).filter(Boolean).join(', ');
// "apple, banana, cherry, date"

The .filter(Boolean) removes empty lines. The .trim() handles any trailing spaces or carriage returns.

Python

lines = """apple
banana
cherry
date"""

csv_line = ', '.join(line.strip() for line in lines.strip().split('\n') if line.strip())
# "apple, banana, cherry, date"

Bash

paste -sd',' file.txt

The paste command with -s (serial) and -d',' (delimiter comma) joins all lines from a file into one comma-separated line. One of those Unix commands that does exactly one thing and does it perfectly.

Or with tr:

cat file.txt | tr '\n' ',' | sed 's/,$//'

The tr replaces newlines with commas, and sed removes the trailing comma.

The CSV Escaping Rules (The Part People Get Wrong)

If you’re generating proper CSV files (not just comma-separated text), there are rules that matter. RFC 4180 is the standard, and here’s what it says:

Rule 1: Fields separated by commas. Simple enough.

Rule 2: Records separated by newlines. Each line is one row.

Rule 3: Fields containing commas, quotes, or newlines must be enclosed in double quotes.

This is the one that catches people. If one of your values is New York, NY, you can’t just put it in the CSV as-is:

city,state,zip
New York, NY,10001    ← WRONG: parser thinks "NY" is a separate field

It needs to be quoted:

city,state,zip
"New York, NY",10001  ← CORRECT

Rule 4: Double quotes inside quoted fields are escaped by doubling them.

If a value contains a literal quote character, you double it:

name,quote
"She said ""hello""","greeting"

The ""hello"" renders as: She said “hello”

Getting this wrong is probably the #1 cause of broken CSV imports worldwide. The file looks fine when you open it in a text editor, but Excel or your database import tool parses it incorrectly because a comma inside a value split the field.

The Semicolon Situation (European CSV)

Something that bites international teams constantly: not everyone uses commas in CSV files.

In countries where the decimal separator is a comma (Germany, France, Spain, Brazil, and most of continental Europe), using commas as field separators would create chaos. Is 1,234 a number or two separate values?

So European versions of Excel default to using semicolons as the CSV delimiter. When a German colleague sends you a “CSV” file and it looks broken in your American Excel, this is usually why.

The fix: when importing, specify the delimiter explicitly. In Excel, use Data > From Text/CSV and choose the correct delimiter. In Google Sheets, it usually auto-detects, but you can use IMPORTDATA or manual import settings.

TextSorter’s Text to CSV tool lets you pick your delimiter (comma, semicolon, tab, pipe, or custom), so you can generate the right format for your audience.

Real World Use Cases

Building SQL queries. You have a list of 50 product IDs and need a WHERE clause:

SELECT * FROM products WHERE id IN (101, 102, 103, 104, 105);

Converting a vertical list to comma-separated and wrapping it in IN () is a workflow that happens thousands of times a day in database teams.

Populating CMS tag fields. WordPress, Shopify, and most CMS platforms accept comma-separated tags. If your keyword researcher gives you a vertical list, you need to convert it.

API parameters. Many REST APIs accept comma-separated values in query parameters: ?ids=1,2,3,4,5. Converting a list to this format is faster with a tool than manual typing.

Email recipient lists. Some email systems accept comma-separated email addresses for CC or BCC fields. Paste a list from a spreadsheet column, convert to commas, done.

Data migration. Moving data between systems often involves reformatting. Source system exports one-per-line, target system expects CSV. The conversion is mechanical but error-prone when done manually with large datasets.

Common Mistakes

Trailing comma. After converting, check if there’s a comma after the last item. Some tools add one, some don’t. A trailing comma in JSON is invalid (except in some JavaScript contexts). In SQL, it’ll cause a syntax error.

Missing quotes around strings. If your values contain commas and you don’t quote them, your CSV structure breaks. Always quote values that might contain the delimiter character.

Mixed delimiters. Deciding to use commas halfway through and semicolons for the rest of the file. Pick one, stay consistent.

Encoding issues. If your list contains characters like accented letters or emoji, make sure you save the output as UTF-8. Some older systems default to ASCII or Latin-1, which mangles non-English characters.

Empty items. If your source list has blank lines, they’ll become empty values in your comma-separated output (like apple,,cherry). Filter them out first.

The Text to CSV tool handles all of these edge cases automatically: it skips empty lines, trims whitespace, and lets you choose your quoting style. Three seconds, clean output, move on.

Convert your list to CSV now →