Python CSV: handling quoted commas and embedded newlines
by nolookshot - Friday October 16, 2026 at 02:39 PM
#1
A CSV record is not always one physical line: a quoted field can contain a comma or a newline. Splitting text with split(",") or splitting all lines before parsing can break valid records.

This single-line Python example uses only the standard library and synthetic data:

import csv, io; rows = list(csv.reader(io.StringIO('item,note\nwidget,"line one\nline two"\ngadget,"a,b"\n', newline=""))); print(len(rows), repr(rows[1][1]), repr(rows[2][1]))

Expected output:

3 'line one\nline two' 'a,b'

For a real UTF-8 file, the following is also one Python line. It counts parsed records, including the header, without collecting the whole file in a list:

with open("example.csv", encoding="utf-8", newline="") as stream: print(sum(1 for row in csv.reader(stream)))

Import csv first, as in the example above. Opening with newline="" lets the CSV parser handle line endings. Set delimiter and quotechar to match the producer if it uses a different dialect. Reader fields are strings by default; convert numeric columns explicitly after parsing.

Reference: https://docs.python.org/3/library/csv.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python pathlib: suffix, suffixes and stem for compound filenames nolookshot 0 187 10-16-2026, 02:41 PM
Last Post: nolookshot
  Python timezone-aware timestamps versus elapsed durations nolookshot 0 100 10-16-2026, 02:40 PM
Last Post: nolookshot
  Retrieving Firefox Cookies Using Python Aurora 15 6,030 09-10-2026, 03:29 AM
Last Post: realtruth
  [Tutorial] Bruteforce Any RDP Using Python 0x666 56 6,542 02-08-2026, 02:17 PM
Last Post: lombardi
  How to have CHATGPT PERPLEXITY and OTHERS for FREE2. Amsterdamer 35 3,288 02-06-2026, 04:53 PM
Last Post: chinaxjp



 Users browsing this thread: