Python Forum
Reading specific rows (lookup)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading specific rows (lookup)
#1
I would like to pull data from a csv, rather than manually enter date range and filename every time.

In my csv, example rows:

2018, 01, 2017-12-31, 2018-01-06, filename_201801W01.csv.gz
2018, 02, 2018-01-07, 2018-01-13, filename_201801W02.csv.gz

At present, I manually enter:

date_ranges = [('2017-12-03', '2017-12-09')]
filename = 'filename_201801W01.csv.gz'


However, I would prefer to have to enter something like:

Year: '2018'
Week: '01'

and have the dates and filename pulled from the csv


Can you help?

So far:

    with open ('DP Python dates.csv') as csvfile:
        readCSV = csv.reader(csvfile)
        datetos = []
        datefroms = []
        filenames = []
        
        for row in readCSV:
            year = [0]
            week = [1]
            datefrom = [2]
            dateto = [3]
            filename = [4]
            
            datefroms.append(datefrom)
            datetos.append(dateto)
            filenames.append(filename)
        
        whatWeek = '01'
        coldex = week.index(whatWeek)
        coldex2  =week.index(whatWeek)
        theDateFrom = datefroms[coldex]
        theDateTo = datetos[coldex2]
        
        date_range = [(theDateFrom, theDateTo)]
Then, I want something like:

week = 01
date_range = [( datefrom, dateto)]
filename = nameoffile

Thanks
Reply
#2
Quote:In my csv, example rows:

2018, 01, 2017-12-31, 2018-01-06, filename_201801W01.csv.gz
2018, 02, 2018-01-07, 2018-01-13, filename_201801W02.csv.gz

At present, I manually enter:

date_ranges = [('2017-12-03', '2017-12-09')]
filename = 'filename_201801W01.csv.gz'


However, I would prefer to have to enter something like:

Year: '2018'
Week: '01'

and have the dates and filename pulled from the csv

Im not sure if its just me or anybody else. I dont understand what your asking. Your question is too wage; hard to understand what your asking. Let me ask you a few questions.

Am i right in thinking that your CSV file looks as follow?

Output:
2018, 01, 2017-12-31, 2018-01-06, filename_201801W01.csv.gz 2018, 02, 2018-01-07, 2018-01-13, filename_201801W02.csv.gz
You want the data range and the file name retrieved when just entered the year and the week? For instance

Input
year: 2018
week: 01

Output:
date_range: 2017-12-31, 2018-01-06 filename: filename_201801W01.csv.gz
Correct?
Reply
#3
Exactly right.

I’m as clear as mustard, as always. Thank you for deciphering
Reply
#4
import csv

def main():
    year = "2018"
    week = "01"
    results = []
    with open("./files/sample.csv", "r") as fh:
        reader = csv.reader(fh, delimiter=',')
        for row in reader:
            row = list(map(str.strip, row))
            if row[0] == year and row[1] == week:
                results.append((row[2] + ":" + row[3], row[4]))
    print(results)


if __name__ == "__main__":
    main()
Something like this
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading Specific Rows In a CSV File finndude 3 940 Dec-13-2022, 03:19 PM
Last Post: finndude
  reading content between a specific xml tag saisankalpj 1 808 Oct-31-2022, 01:37 PM
Last Post: wavic
  Pymysql delete specific rows in tableview stsxbel 2 1,049 Aug-18-2022, 09:50 AM
Last Post: ibreeden
  Trying to delete rows above a specific datetime value cubangt 19 11,002 May-09-2022, 08:57 PM
Last Post: deanhystad
  2-dataframe, datetime lookup problem Mark17 0 1,215 Jan-27-2022, 01:02 AM
Last Post: Mark17
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,599 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Python VLookup? Lookup Table? Nu2Python 3 2,373 Oct-25-2021, 08:47 PM
Last Post: Nu2Python
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,080 May-04-2021, 10:51 PM
Last Post: rhat398
  Can I replace IF statements with a key lookup ? jehoshua 3 2,460 Mar-05-2021, 10:24 PM
Last Post: jehoshua
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 6,958 Mar-02-2021, 01:54 AM
Last Post: Jeremy7

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020