Your import reports one customer successfully loaded. The email address looks valid. Nothing crashes. Yet one of the two addresses in the source file has vanished from the record your script passes onwards.
This can happen when a CSV contains two columns with the same heading and an AI-written Python script reads them into a dictionary. The danger is easy to miss because the number of customer rows can remain correct. The loss happens inside each row, at the point where column names become keys.
For creators moving mailing lists, owners combining contact exports and anyone checking generated automation, the useful question is precise: does every source column still have a distinct meaning after conversion? A successful run is not enough to answer it.
Two columns, one dictionary key
Imagine a hypothetical export with three headings: customer,email,email. Its first data record is C17,first@example.test,second@example.test. Perhaps somebody joined two contact exports and forgot to distinguish primary and alternative addresses.
Python’s official CSV documentation explains that DictReader maps row information to a dictionary and, unless field names are supplied, takes those names from the first row. That convenience depends on the headings being suitable keys for the intended record.
import csv
from io import StringIO
text = "customer,email,email\nC17,first@example.test,second@example.test\n"
reader = csv.DictReader(StringIO(text))
print(reader.fieldnames)
print(next(reader))
In a local check using Python 3.12.14, the headings remained ['customer', 'email', 'email'], but the returned row was {'customer': 'C17', 'email': 'second@example.test'}. The earlier address was absent from that dictionary. The original text still contained both addresses; the converted record did not.
This result fits Python’s documented dictionary construction behaviour: when a key appears repeatedly in the input pairs, its last value is retained. There is no second, separately addressable email entry in the resulting dictionary.
Why the usual reassuring checks miss it
Suppose the importer verifies that it received one row and found a non-empty value under email. Both checks pass in this example. They answer whether a record exists and whether its surviving email field is populated. Neither compares the source columns with the destination meaning.
Checking the output alone is especially weak here. Once the row has become a dictionary, printing it does not reveal the overwritten address. To investigate, compare the header and original record with the representation immediately after parsing, before any deduplication or database write.
The lesson is not that an AI assistant must have made this mistake. It is that a proposed script needs a test containing the troublesome structure. A clean sample with unique headings cannot demonstrate how the same script handles repeated ones.
Reject ambiguous headings before reading data rows
For an importer that requires unique, non-blank column names, a small preliminary gate can stop the example before conversion proceeds. Here is an illustrative pattern; its deliberate policy is to reject ambiguity rather than choose which address the business wanted.
reader = csv.DictReader(StringIO(text))
names = reader.fieldnames
if not names or any(not name.strip() for name in names):
raise ValueError("Missing or blank column name")
if len(names) != len(set(names)):
raise ValueError("Duplicate column name")
# Process reader only after these checks pass.
The fieldnames attribute is available before consuming the first data record; the documentation says that accessing it initialises it when necessary. In the local check, this gate rejected duplicate headings, a blank heading and an empty input, while accepting a simple file with distinct headings.
That is a narrow check. It does not validate addresses, identify the right customer or establish that the file has all the required business columns. Keep those requirements separate and explicit. For a real import, the expected header names should come from the agreed source format, not from an AI assistant’s guess.
Cleaning names can create a second collision
Now imagine headings Email and email . The exact-name gate above treats them as different. If a later stage trims spaces and lowercases both names, they become the same key.
Decide first whether such normalisation is part of your import contract. If it is, check uniqueness again after applying that exact transformation and before using the transformed names as keys. Preserve the original headings so that any rejection can explain which source columns collided.
Do not silently rename the second address email_2 and assume the problem is solved. That preserves a place for a value but leaves its role unresolved. In this hypothetical contact export, the owner needs to decide whether the columns represent a primary address, an alternative address or conflicting versions of the same field.
Make the rejection actionable. A useful report would identify the source file, the repeated heading and its column positions, while leaving the original file available for correction. Listing positions matters when repeated names are far apart in a wide export. The person fixing the file should be able to locate both columns without inspecting every customer’s contact details.
Test a duplicate pair separated by an unrelated column as well as adjacent duplicates. Also test distinct headings that happen to contain identical values: those are not duplicate column names. The gate should reject ambiguity in the header, without treating an ordinary repeated value as proof that a column should disappear.
Give the AI a testable brief
A useful request would be: “This importer requires unique, non-blank column headings. Stop before processing data records if that requirement fails. Show the offending heading positions and do not automatically select a winning column. Demonstrate a repeated-heading case and a valid case.”
Ask for the small example first. Inspect the source text, the parsed headings, the rejection and the valid output. If the import will create or update customer records, trial it without writing those changes until the header policy is settled. This is a recommended review process, not a claim that the short snippet is a complete production importer.
Also keep row-shape checks. MaryChuks.com’s guide to missing CSV fields versus empty fields covers a different failure: a heading can be valid while an individual record lacks its corresponding value. Passing the header gate does not settle that question.
Before approving an import, follow one difficult source record all the way to its destination. Count its meanings as well as its rows. Two addresses under an ambiguous repeated heading need an explicit decision before a convenient data structure makes that decision for you.
Sources and verification
Primary references: Python’s CSV module documentation and dictionary documentation. These are versioned technical references, not news announcements. The example and header checks were run locally with Python 3.12.14. Business scenarios and review recommendations are illustrative.
Discover more from Marychuks.com AI, Psychology, Business & CreativeVerse
Subscribe to get the latest posts sent to your email.
One thought on “AI-Written CSV Imports: Two Columns Can Become One Field”