# Export data to CSV with Python

Learn two ways to write CSV files from Python, with pandas to_csv or the built-in csv module, and how to set encoding so files open cleanly.

You can export data to CSV with Python through the pandas to_csv method or the built-in csv module. CSV, short for comma-separated values, stores each record as one line of text. Pandas suits data already held in a DataFrame, while the csv module needs no extra install. Both approaches let you control the delimiter, the header row and the file encoding. 

## Writing a DataFrame with pandas to_csv 

The pandas to_csv method writes a whole DataFrame to a CSV file in one call. A typical call looks like df.to_csv('listings.csv', index=False), which skips the row index column. Without index=False, pandas adds an unnamed first column holding the row numbers. The sep parameter changes the delimiter, for example to a semicolon for spreadsheets that expect one. 

## Writing rows with the csv module 

The csv module in the Python standard library writes CSV files without any third-party package. Open the file with newline='' and pass it to csv.writer or csv.DictWriter. The writerow method adds one list at a time, and writerows adds many lists at once. DictWriter takes dictionaries and a fieldnames list, then writes the header with writeheader. 

## Choosing the encoding parameter 

The encoding parameter decides how Python turns text into bytes when writing a CSV file. UTF-8 handles accented letters and non-Latin scripts in names, addresses and notes. Some spreadsheet programs read plain UTF-8 files incorrectly and show garbled characters. Setting encoding='utf-8-sig' adds a byte order mark, a short marker that helps those programs detect UTF-8. 

## Questions about exporting CSV in Python 

### Should I use pandas or the csv module to export a CSV? 

Pandas is the shorter route when your data already sits in a DataFrame. The csv module fits small scripts, or environments where you cannot install extra packages. Pandas also handles missing values, dates and column order with simple parameters. The csv module gives you row-by-row control and keeps memory use low for streamed data. 

### Why does my CSV file have blank lines between rows? 

Blank lines between rows appear when a file opened for the csv module lacks newline=''. The csv writer adds its own line endings, and the operating system may add another. On Windows this produces an extra empty line after every record. Passing newline='' to the open() function stops the doubled line break. 

### How do I append rows to an existing CSV file? 

Appending rows to an existing CSV file works by opening it in append mode. In the csv module, open the file with mode 'a' and newline='' before writing. In pandas, call to_csv with mode='a' plus header=False to avoid a repeated header row. Check that the new rows use the same column order as the existing file. 

### How do I export only some columns from a DataFrame? 

Selected DataFrame columns can be exported by passing a list to the columns parameter of to_csv. For example, df.to_csv('shortlist.csv', columns=['name', 'rating'], index=False) writes just those two named fields. The listed order also sets the column order in the output file. Trimming columns helps when a comparison shortlist needs only a few fields from a larger table.
