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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:07:04+00:00 2026-06-10T20:07:04+00:00

I am working with a CSV that has the following structure: 2012-09-01 20:03:15,http://example.com The

  • 0

I am working with a CSV that has the following structure:

"2012-09-01 20:03:15","http://example.com"

The data is a cleaned up dump of my browsing history. I am interested in counting the first five unique domains per a given day. Here is what I have so far:

from urlparse import urlparse
import csv
from collections import Counter

domains = Counter()

with open("history.csv") as f:
    for row in csv.reader(f):
        d = row[0]
        dt = d[11:19]
        dt = dt.replace(":","")
        dd = d[0:10]
        if (dt < "090000") and (dt > "060000"):
            url = row[1]
            p = urlparse(url)
            ph = p.hostname
            print dd + "," + dt + "," + ph
            domains += Counter([ph])
t = str(domains.most_common(20))

With d, dt, and dd, I am separating the date and time. With the above example row, dt = 20:03:15, and dd = 2012-09-01. The “if (dt < “090000”) and (dt > “060000”)” is just to say that I am only interested in counting websites visited between 6am and 9am. How would I say “count only the first five websites that were visited before 6am, each day”? There are hundreds of rows for any given day, and the rows are in chronological order.

  • 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-10T20:07:05+00:00Added an answer on June 10, 2026 at 8:07 pm
    import csv
    from collections import defaultdict, Counter
    from datetime import datetime
    from urlparse import urlsplit
    
    indiv = Counter()
    
    domains = defaultdict(lambda: defaultdict(int))
    with open("history.csv", "rb") as f:
        for timestr, url in csv.reader(f):
            dt = datetime.strptime(timestr, "%Y-%m-%d %H:%M:%S")
            if 6 <= dt.hour < 11: # between 6am and 11am
                today_domains = domains[dt.date()]
                domain = urlsplit(url).hostname
                if len(today_domains) < 5 and domain not in today_domains:
                    today_domains[domain] += 1
                    indiv += Counter([domain])
    for domain in indiv:
        print '%s,%d' % (domain, indiv[domain])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a CSV upload that has 35,000 lines of stuff going on
I exported some data in CSV format that has some line breaks within text
I'm working with 6 csv files that each contain the attributes of an object.
I have a CSV file that I'm working with, and all the fields are
I'm working some code that inserts csv rows into an SQLite database using Python.
I am dealing with a csv file that has some customer information (email, name,
I'm working on a C# project that has a post-build event command line that
I'm working on an application that has to make specific decisions based on files
I'm currently working on a project that involves a CSV file. I initially was
I have some imported csv data that I have turned into an xts object.

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.