简体   繁体   中英

Selecting all rows with dates in a unstructured csv

So I have the output of a Google Trends query. It contains several tables on one sheet. The first part of the sheet looks like:

Web Search interest: nespresso  
United States; date_range:(today 90-d)  

Interest over time  
Day nespresso
8/7/2015    70
8/8/2015    82
8/9/2015    91
8/10/2015   84

So here's what I'd like to do. Disregard the first few rows and select any rows with a date. (weekly data from have date as 8/7/2015-8/14/2015). Sure, there's nrow and skip in read.csv, but I was wondering if there was a systematic way to do this.

Also, bear in mind that the data from Google trends includes data after the dates.

11/3/2015    
11/4/2015    


Top subregions for nes  
Subregion   nes
New York    100
Massachusetts   83

Looking for Python or R solution

Consider this Python solution to read in raw csv and convert first column to date. Try/Except is used to skip rows that do not convert properly to date format.

import csv
from datetime import datetime

with open('Unstructured.csv', 'rt') as csvfile:
    csvReader = csv.reader(csvfile)
    data = []

    for row in csvReader:
        try:
            data.append([datetime.strptime(row[0], "%m/%d/%Y").strftime("%Y-%m-%d"), row[1]])
        except ValueError:
            continue

    for i in data:
        print(i)

Output (data list)

['2015-08-07', '70']
['2015-08-08', '82']
['2015-08-09', '91']
['2015-08-10', '84']
['2015-11-03', '']
['2015-11-05', '']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM