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 8380103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:22:18+00:00 2026-06-09T16:22:18+00:00

I’m in the process of writing a script that chunks a large CSV file

  • 0

I’m in the process of writing a script that chunks a large CSV file down into smaller chunked files. It cross references a log file that holds the last timestamp that was chunked so that only the timestamps that are later than the logged time are written/chunked.

The first column of the csv file has a timestamp that is in %Y%m%d %H%M%S form. The CSV file also has four rows of header information that I don’t want/need in my script, which the rows in ts_pre clause removes.

The log_lookup() function just pulls the last timeseries from the log for a particular station’s CSV file that I’m viewing. Obviously, I’m working with six different stations that all have different columns of information, except they all share the same structure as I described in the second paragraph.

The partial script is:

import csv, sys, datetime

def log_lookup():  
    global STN_num
    global STN_date
    with open('/home/log.txt', 'rb') as open_log:    
        log_file = csv.reader(open_log)
            for row in log_file:
                for item in row:
                     STN_date.append(item)
            if find == 'STN_1':
                return STN_date[1]
            if find == 'STN_2':           
                return STN_date[2]
            if find == 'STN_3':
                return STN_date[3]
            if find == 'STN_4':
                return STN_date[4]
            if find == 'STN_5':
                return STN_date[5]
            if find == 'STN_6':
                return STN_date[6]

def get_ts(line):
    print line[0:19]
    return datetime.datetime.strptime(line, "%Y/%m/%d %H:%M:%S")

def main():
    log = str(log_lookup()) #useful for knowing when to start chunking 
    log_datetime = datetime.datetime.strptime(log, "%Y/%m/%d %H:%M:%S")       

    with open(sys.argv[1], 'rb') as open_file: 
        ts_from_file = csv.reader(open_file)
        for genrows in ts_from_file:            
            ts_pre.append(genrows)  
            for rows in ts_pre:             
                if rownum < 4:
                    ts_pre.pop()
                    rownum += 1
                else:       
                    for line in rows:                            
                        if get_ts(line) > log_datetime:                 
                            timeseries.append(line)

The log file is simply:

0
2011/10/06 18:40:00
2012/06/27 13:25:00
1900/01/01 00:00:00
2011/08/03 14:55:00
2012/06/27 20:05:00
2011/10/03 19:25:00

… with 0 as a placeholder. (Is it obvious I’m not a programmer?)

An example CSV file looks like:

"2011/10/03 16:40:00",0,0
"2011/10/03 16:45:00",1,0
"2011/10/03 16:50:00",2,0
"2011/10/03 16:55:00",3,0

The error I’m getting when the ts_line(line) function is that it’s saying line[0:19] is:

2011/10/03 16:40:00
0

and the function returns 0 and Python throws this error:

ValueError: time data '0' does not match format '%Y/%m/%d %H:%M:%S'

I have verified that the 0 that’s returned is the second item in the CSV file, but I’m confused as to why Python returns it at all in my slice selection. Can someone explain to me why it’s returning that value and what I need to do to get the timestamp to compare to the log timestamp?

For extra credit, any advice on coding/style is always appreciated and/or advice for better ways accomplish what I’m doing. The CSV files I’m looking at are quite large (~8 MB) so the more efficient the better.

  • 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-06-09T16:22:20+00:00Added an answer on June 9, 2026 at 4:22 pm

    It looks like you are calling get_ts for each column in every line of the csv.

    Instead of

    for line in rows:                            
        if get_ts(line) > log_datetime:                 
            timeseries.append(line)
    

    Try:

    if get_ts(line[0]) > log_datetime:                 
        timeseries.append(line)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.