Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6544609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:29:11+00:00 2026-05-25T11:29:11+00:00

I have about 100 CSV files I have to operate on once a month

  • 0

I have about 100 CSV files I have to operate on once a month and I was trying to wrap my head around this but I’m running into a wall. I’m starting to understand some things about Python, but combining several things is still giving me issues, so I can’t figure this out.

Here’s my problem:

I have many CSV files, and here’s what I need done:

add a “column” to the front of each row (or the back, doesn’t matter really, but front is ideal). In addition, each line has 5 rows (not counting the filename that will be added), and here’s the format:

6-digit ID number,YYYY-MM-DD(1),YYYY-MM-DD(2),YYYY-MM-DD(3),1-2-digit number

I need to subtract YYYY-MM-DD(3) from YYYY-MM-DD(2) for every line in the file (there is no header row), for every CSV in a given directory.

I need the filename inside the row because I will combine the files (which, if is included in the script would be awesome, but I think I can figure that part out), and I need to know what file the records came from. Format of filename is always ‘4-5-digit-number.csv’

I hope this makes sense, if it does not, please let me know. I’m kind of stumped as to where to even begin, so I don’t have any sample code that even really began to work for me. Really frustrated, so I appreciate any help you guys may provide, this site rocks!

Mylan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:29:12+00:00Added an answer on May 25, 2026 at 11:29 am

    There’s a tool in the standard library for each of these tasks:

    To iterate over all CSV files in a directory, use the glob module:

    import glob
    for csvfilename in glob.glob(r"C:\mydirectory\*.csv"):
        #do_something
    

    To parse a CSV file, use the csv module:

    import csv
    with open(csvfilename, "rb") as csvfile:
        reader = csv.reader(csvfile, delimiter=",")
        for row in reader:
            # row is a list of all the entries in the current row
    

    To parse a date and calculate a difference, use the datetime module:

    from datetime import datetime
    startdate = datetime.strptime("1999-10-20", "%Y-%m-%d")
    enddate = datetime.strptime("2003-02-28", "%Y-%m-%d")
    delta = enddate - startdate # difference in days
    

    To add a value to the beginning of a row:

    row[0:0] = [str(delta)]
    

    To append the filename to the end of a row:

    row.append(csvfilename)
    

    And to write a row to a new CSV file:

    with open(csvfilename, "wb") as csvfile:
        writer = csv.writer(csvfile, delimiter=",")
        writer.writerow(row)
    

    Taken all together, you get:

    import glob
    import csv
    from datetime import datetime
    
    with open("combined_files_csv", "wb") as outfile:
        writer = csv.writer(outfile, delimiter=",")
        for csvfilename in glob.glob(r"C:\mydirectory\*.csv"):
            with open(csvfilename, "rb") as infile:
                reader = csv.reader(infile, delimiter=",")
                for row in reader:
                    startdate = datetime.strptime(row[3], "%Y-%m-%d")
                    enddate = datetime.strptime(row[2], "%Y-%m-%d")
                    delta = enddate - startdate # difference in days
                    row[0:0] = [str(delta)]
                    row.append(csvfilename)
                    writer.writerow(row)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have about 20 .csv files which are around 100-200mb each. They each have
I have about a 100 csv files each 100,000 x 40 rows columns. I'd
I have about 100 elements in like and am trying to create an animation
I have about 100 data points which mostly satisfying a certain function (but some
I have 20 large CSV (100-150MB each) files i would like to load in
I have about 100 error marker 50 each message. But not every Entity has
I have about 100 .txt files that contain plain text. Somehow, some of the
I have 500 csv files , each of them's size is about 10~20M. for
I have about 100 Word documents which include transliteration of foreign names. The author
I have about 100 tables where all of them have duplicate foreign key constraints

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.