How to create a CSV file
The easiest way to create a CSV file is to type your data in Excel or Google Sheets, one row per record with column names in the first row, then save or download it as CSV. You can also write it by hand in any text editor: one line per row, values separated by commas, and the file saved with a .csv extension in UTF-8. Here is how in each app, plus the few rules that make a CSV import without errors.
Last updated
What the file should look like
A CSV file is plain text. The first line holds the column names, every other line is one record, and commas separate the values:
name,email,city,amount
Ana García,[email protected],Madrid,120.50
"Lee, Min",[email protected],Seoul,89.00
Tom O'Neil,[email protected],"Portland, OR",0Every line has the same number of values, and values that contain a comma are wrapped in double quotes. More on the format in What is a CSV file?
In Excel
- Open a blank workbook. Type the column names in row 1, one per column.
- Type or paste your data from row 2 down, one record per row.
- Choose File > Save As (File > Save a Copy in Microsoft 365) and pick CSV UTF-8 (Comma delimited) (*.csv).
- Click Save, then confirm that only the active sheet is saved.
Codes with leading zeros (zip codes, product codes) are saved correctly only if Excel shows the zeros: format those columns as Text before you type them.
In Google Sheets
- Create a sheet and type the column names in row 1, then your data below.
- Choose File > Download > Comma-separated values (.csv).
The file is saved in UTF-8 with commas, which almost every program accepts.
In Numbers on a Mac
- Create a blank spreadsheet and enter the data, with a header row.
- Choose File > Export To > CSV, open Advanced Options and keep Text Encoding on Unicode (UTF-8).
- Click Save.
Already have a .numbers file? Numbers to CSV converts it on any computer, Windows included.
In a text editor (Notepad, TextEdit)
- Notepad (Windows): type the lines, choose File > Save As, set Save as type to All files, name the file contacts.csv and set Encoding to UTF-8.
- TextEdit (Mac): choose Format > Make Plain Text first, type the lines, save, then rename the file so it ends in .csv (untick “If no extension is provided, use .txt”).
- VS Code or any code editor: create a new file named data.csv. Editors save in UTF-8 by default.
Quote any value that contains a comma, a double quote or a line break, and double the quotes inside it:
id,comment
1,"She said ""yes"" at once"
2,"Line one
Line two"Have the data as plain text already, with tabs, spaces or pipes between the values? TXT to CSV turns it into a proper CSV, quotes included.
With Python
The built-in csv module handles quoting for you. Open the file with newline="" to avoid blank lines on Windows:
import csv
rows = [
["name", "email", "city"],
["Ana García", "[email protected]", "Madrid"],
["Lee, Min", "[email protected]", "Seoul"],
]
with open("contacts.csv", "w", newline="", encoding="utf-8") as f:
csv.writer(f).writerows(rows)A CSV file full of test data
For a demo, a load test or an import trial, you rarely want to type rows by hand. The CSV Generator makes realistic fake data: names, emails that match the names, phone numbers, cities, dates, prices and your own lists of values, from 10 to 1,000,000 rows.
Check the file before you import it
| Mistake | What happens | Fix |
|---|---|---|
| No header row | Columns get mapped wrong | Put the column names in line 1 |
| A comma inside an unquoted value | The row gets an extra column | Wrap the value in double quotes |
| Saved as ANSI, not UTF-8 | Accents turn into ? or é | Save again in UTF-8 |
| File named contacts.csv.txt | The app does not see a CSV | Show file extensions and rename |
| Blank lines at the end | Empty records or an error | Delete them |
The CSV Validator finds broken quotes, rows with the wrong number of columns and encoding problems, and shows the exact line. To look at the file as a table, open it in the CSV Viewer.
Frequently asked questions
How do I create a CSV file from Excel?
Type or paste your data with column names in row 1, then choose File > Save As (Save a Copy in Microsoft 365) and pick CSV UTF-8 (Comma delimited). Only the active sheet is saved.
How do I make a CSV file in Notepad?
Write one row per line with values separated by commas, then choose File > Save As, set Save as type to All files, type a name ending in .csv (for example contacts.csv), set Encoding to UTF-8 and click Save.
How do I create a CSV file on a Mac?
In Numbers, choose File > Export To > CSV. In Excel for Mac, use File > Save As with the CSV UTF-8 format. In TextEdit, choose Format > Make Plain Text before typing, then save and rename the file to end in .csv.
What if a value contains a comma?
Put the whole value in double quotes: "Portland, OR". If the value itself contains a double quote, write it twice inside the quotes: "She said ""yes""". Excel and Google Sheets do this for you when they save a CSV.
Should I use a comma or a semicolon?
Use a comma unless the program you import into asks for a semicolon or tab. Many European versions of Excel expect semicolons because they use the comma as the decimal mark.
How do I create a CSV file with test data?
Use a generator: pick columns such as names, emails, dates and prices, choose the number of rows and download the file. PrivateCSV's CSV Generator makes up to a million rows in the browser.