<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Looplylabs</title>
    <description>The latest articles on DEV Community by Looplylabs (@looplylabs).</description>
    <link>https://dev.to/looplylabs</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3786640%2Ff2822d75-19d9-4ca5-93c8-0a54040bdf05.png</url>
      <title>DEV Community: Looplylabs</title>
      <link>https://dev.to/looplylabs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/looplylabs"/>
    <language>en</language>
    <item>
      <title>What Most CSV Ingestion Scripts Get Wrong (And How to Fix It)</title>
      <dc:creator>Looplylabs</dc:creator>
      <pubDate>Mon, 02 Mar 2026 17:17:48 +0000</pubDate>
      <link>https://dev.to/looplylabs/what-most-csv-ingestion-scripts-get-wrong-and-how-to-fix-it-el8</link>
      <guid>https://dev.to/looplylabs/what-most-csv-ingestion-scripts-get-wrong-and-how-to-fix-it-el8</guid>
      <description>&lt;p&gt;Most CSV ingestion scripts are written in 30 minutes.&lt;/p&gt;

&lt;p&gt;Most ingestion failures take 3 months to notice.&lt;/p&gt;

&lt;p&gt;The problem isn’t CSV.&lt;/p&gt;

&lt;p&gt;The problem is missing guarantees.&lt;/p&gt;

&lt;p&gt;In small teams, CSV ingestion often looks like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Read file&lt;/li&gt;
&lt;li&gt;Loop rows&lt;/li&gt;
&lt;li&gt;Insert into database&lt;/li&gt;
&lt;li&gt;Print “Done”&lt;/li&gt;
&lt;li&gt;It works.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Until the export format changes.&lt;br&gt;
Until the file is empty.&lt;br&gt;
Until duplicates accumulate.&lt;br&gt;
Until a partial insert corrupts reporting.&lt;/p&gt;

&lt;p&gt;Here’s what most ingestion scripts get wrong.&lt;/p&gt;


&lt;h2&gt;
  
  
  1. They Don’t Validate Structure Explicitly
&lt;/h2&gt;

&lt;p&gt;Many scripts assume the column order never changes.&lt;/p&gt;

&lt;p&gt;That assumption eventually breaks.&lt;/p&gt;

&lt;p&gt;Instead of trusting positional mapping, validate headers explicitly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;EXPECTED_HEADERS = [
    "date",
    "customer_id",
    "amount",
    "currency",
    "status"
]

if headers != EXPECTED_HEADERS:
    raise ValueError("Schema mismatch detected")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Order-sensitive comparison is intentional.&lt;/p&gt;

&lt;p&gt;If upstream changes, ingestion should stop immediately.&lt;/p&gt;

&lt;p&gt;Silent drift is worse than a crash.&lt;/p&gt;


&lt;h2&gt;
  
  
  2. They Don’t Sanity-Check Volume
&lt;/h2&gt;

&lt;p&gt;An empty CSV import should not succeed.&lt;/p&gt;

&lt;p&gt;A report with 12 rows instead of 1,200 should not quietly pass.&lt;/p&gt;

&lt;p&gt;Add simple guardrails:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if len(rows) == 0:
    raise RuntimeError("Empty export detected")

if len(rows) &amp;lt; expected_minimum:
    raise RuntimeError("Suspiciously low record count")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Most data corruption is not catastrophic.&lt;/p&gt;

&lt;p&gt;It’s gradual.&lt;/p&gt;


&lt;h2&gt;
  
  
  3. They Allow Partial Inserts
&lt;/h2&gt;

&lt;p&gt;Without transactions, one failed row can leave the database in an inconsistent state.&lt;/p&gt;

&lt;p&gt;Use transactional boundaries:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import psycopg2

with conn:
    with conn.cursor() as cur:
        for row in rows:
            cur.execute(
                "INSERT INTO reports VALUES (%s,%s,%s,%s,%s)",
                row
            )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If one insert fails, the entire operation rolls back.&lt;/p&gt;

&lt;p&gt;No half-written states.&lt;/p&gt;


&lt;h2&gt;
  
  
  4. They Ignore Idempotency
&lt;/h2&gt;

&lt;p&gt;If a job runs twice, does it duplicate records?&lt;/p&gt;

&lt;p&gt;If yes, retries become dangerous.&lt;/p&gt;

&lt;p&gt;Use unique constraints and upserts:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO reports (id, amount)
VALUES (%s, %s)
ON CONFLICT (id)
DO UPDATE SET amount = EXCLUDED.amount;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Idempotency turns retries from risk into safety.&lt;/p&gt;


&lt;h2&gt;
  
  
  5. They Depend on Humans
&lt;/h2&gt;

&lt;p&gt;If someone manually uploads a CSV every week, the workflow depends on memory.&lt;/p&gt;

&lt;p&gt;Replace the ritual with scheduling:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;0 2 * * 1 /usr/bin/python3 ingest_reports.py &amp;gt;&amp;gt; ingestion.log 2&amp;gt;&amp;amp;1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Humans forget.&lt;/p&gt;

&lt;p&gt;Cron does not.&lt;/p&gt;


&lt;h2&gt;
  
  
  The Underlying Principle
&lt;/h2&gt;

&lt;p&gt;Automation is not about execution.&lt;/p&gt;

&lt;p&gt;It’s about deterministic state transitions.&lt;/p&gt;

&lt;p&gt;A safe ingestion pipeline guarantees:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Structural integrity&lt;/li&gt;
&lt;li&gt;Volume sanity&lt;/li&gt;
&lt;li&gt;Atomic writes&lt;/li&gt;
&lt;li&gt;Safe retries&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Everything else is optimism.&lt;/p&gt;

&lt;p&gt;Further Reading&lt;/p&gt;

&lt;p&gt;I wrote a deeper breakdown of deterministic ingestion architecture — including file archival, observability, and production safeguards — here:&lt;/p&gt;

&lt;p&gt;

&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
        &lt;div class="c-embed__cover"&gt;
          &lt;a href="https://www.looplylabs.com/blog/deterministic-csv-ingestion-pipeline-python/" class="c-link align-middle" rel="noopener noreferrer"&gt;
            &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.looplylabs.comhttps%3A%2F%2Fcdn.looplylabs.com%2Fstatic%2Fbrand%2Fassets%2Fog-1200x630.png" height="auto" class="m-0"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="c-embed__body"&gt;
        &lt;h2 class="fs-xl lh-tight"&gt;
          &lt;a href="https://www.looplylabs.com/blog/deterministic-csv-ingestion-pipeline-python/" rel="noopener noreferrer" class="c-link"&gt;
            
        
        
            Automating CSV to PostgreSQL Safely Using Python (Determini… | Looplylabs
        
        
    
          &lt;/a&gt;
        &lt;/h2&gt;
          &lt;p class="truncate-at-3"&gt;
            Learn how to replace fragile manual CSV imports with a deterministic Python ingestion pipeline using schema validation, row verification, transactions, and sch…
          &lt;/p&gt;
        &lt;div class="color-secondary fs-s flex items-center"&gt;
            &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.looplylabs.com%2Fstatic%2Fbrand%2Ffavicons%2Flyp%2Ffavicon.svg"&gt;
          looplylabs.com
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;





</description>
      <category>python</category>
      <category>postgres</category>
      <category>backend</category>
      <category>automation</category>
    </item>
  </channel>
</rss>
