DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Configure AWS Glue Job Using Python-Based AWS CDK
  • Giving AI Agents Safe Access to Legacy Systems: Introducing legacy2mcp
  • Adding Graph Retrieval to a RAG Pipeline With Python
  • Building a Product Recommendation Engine With Neo4j — No ML Library Required

Trending

  • When 12 Hours of Offline Changes Collide: Designing Conflict-Safe Mobile Sync
  • Evaluating RAG in Production — What the Numbers Actually Revealed
  • Constrained Decoding Models Explained: Encoders, Decoders and When to Use Them
  • Building a Real Pipeline With senro: Monorepos, Caching, and Triggers

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. Splitting CSV Files in Python

Splitting CSV Files in Python

In this article, see a code snippet that splits CSV files in Python.

By 
Zehra Can
·
Oct. 08, 20 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
71.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Sometimes it is necessary to split big files into small ones. In this brief article, I will share a small script which is written in Python. Maybe you can develop it further. However, if the file size is bigger you should be careful using loops in your code.

Let's assume your input file name input.csv. The code will split it into new files with names input_1.csv, ..., input_10.csv.,...

Python
 




x


 
1
import pandas as pd
2
 
          
3
#csv file name to be read in 
4
in_csv = 'input.csv'
5
 
          
6
#get the number of lines of the csv file to be read
7
number_lines = sum(1 for row in (open(in_csv)))
8
 
          
9
#size of rows of data to write to the csv, 
10
#you can change the row size according to your need
11
rowsize = 500
12
 
          
13
#start looping through data writing it to a new file for each set
14
for i in range(1,number_lines,rowsize):
15
    df = pd.read_csv(in_csv,
16
          header=None,
17
          nrows = rowsize,#number of rows to read at each loop
18
          skiprows = i)#skip rows that have been read
19
 
          
20
    #csv to write data to a new file with indexed name. input_1.csv etc.
21
    out_csv = 'input' + str(i) + '.csv'
22
 
          
23
    df.to_csv(out_csv,
24
          index=False,
25
          header=False,
26
          mode='a',#append data to csv file
27
          chunksize=rowsize)#size of data to append for each loop



Python (language) CSV

Opinions expressed by DZone contributors are their own.

Related

  • How to Configure AWS Glue Job Using Python-Based AWS CDK
  • Giving AI Agents Safe Access to Legacy Systems: Introducing legacy2mcp
  • Adding Graph Retrieval to a RAG Pipeline With Python
  • Building a Product Recommendation Engine With Neo4j — No ML Library Required

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook