DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk3 min

Python List File Formats: What to Know About Text, JSON, and CSV

Write list items one per line for readable text, use JSON to preserve supported structure, or choose CSV for rows and columns. Examples use Python’s standard library.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use plain text to put one readable item on each line, JSON to save a list’s supported types and structure for loading later, or CSV when the destination needs rows and columns. In each case, Python’s standard library can write the file; the right method depends on how you’ll use it.

Write a list to a plain-text file, one item per line

For a simple text file, open it in write mode, convert each item to a string, and add a newline after it. Explicit UTF-8 encoding and a with block make the file handling predictable. These examples follow the Python 3.14.7 documentation.

As an Amazon Associate I earn from qualifying purchases.

items = ["apples", "bananas", "cherries"]

with open("items.txt", "w", encoding="utf-8") as file:
    for item in items:
        file.write(f"{item}n")

file.write() accepts strings, so the f-string converts each value to its text form. You can write str(item) instead if you prefer to make that conversion explicit. This approach is readable, but it does not preserve the original Python types: for example, a number written as text is not automatically restored as a number when the file is read.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use writelines() for a list of strings

For string items, you can write the lines with a generator expression:

items = ["apples", "bananas", "cherries"]

with open("items.txt", "w", encoding="utf-8") as file:
    file.writelines(item + "n" for item in items)

writelines() does not add line breaks for you. Include n in each string, or the values will run together.

Save a Python list as JSON to preserve its structure

Choose JSON when you want to load the list back as data and retain supported types and nesting, rather than just store a human-readable rendering. Python’s json module writes the list with json.dump() and reads it with json.load().

import json

items = [1, "simple", ["nested", "values"]]

with open("items.json", "w", encoding="utf-8") as file:
    json.dump(items, file, ensure_ascii=False, indent=2)

with open("items.json", encoding="utf-8") as file:
    restored_items = json.load(file)

JSON supports built-in data types such as lists and dictionaries; arbitrary class instances need to be converted to supported data first. The example uses UTF-8, as required for JSON files by Python’s documentation. Write one complete JSON document per file: JSON is not a framed protocol, so calling json.dump() repeatedly on the same file does not create a valid sequence of independent JSON documents.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Write a list as CSV rows

Use the csv module when your data is tabular and needs rows and columns, such as for a spreadsheet or another CSV-aware program. Here, each inner list becomes one row:

import csv

rows = [["name", "score"], ["Ada", 98], ["Lin", 95]]

with open("scores.csv", "w", newline="", encoding="utf-8") as file:
    writer = csv.writer(file)
    writer.writerows(rows)

For a list of dictionaries, use csv.DictWriter, specify the field names, write a header if needed, and call writerows() for the records. Open CSV files with newline=""; the CSV module handles delimiters and quoting according to its dialect. Avoid joining fields with commas yourself, because values containing commas, quotes, or newlines need proper CSV quoting.

Choose the format that fits the next step

Format Best for What it preserves
Plain text, one item per line Reading or editing a simple list Text representations only; not the original types or nested structure
JSON Loading supported data back into Python or another JSON-capable program Supported types and list/dictionary structure
CSV Rows and columns for spreadsheets or tabular interchange Tabular values and fields, not arbitrary Python object types

Prevent common file-writing mistakes

  • Non-string items in a text file: convert each item with str() or an f-string. Choose JSON or CSV instead if you need structured data.
  • Missing line separators: add n yourself when using writelines().
  • Overwriting existing content: mode "w" creates a file or truncates it before writing. Use "a" when you intend to append instead.
  • Unspecified text encoding: set encoding="utf-8" unless your use case requires another encoding.
  • File not properly closed: use with; Python closes the file when the block ends, including when an exception occurs.
  • CSV assembled by hand: use csv.writer or csv.DictWriter so delimiters, quotes, and newlines are handled as CSV.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.