开发者

defining parsing range for a CSV

开发者 https://www.devze.com 2023-02-07 09:41 出处:网络
I`m importing a CSV containing data from year 2002 to 2011 , using this : pair = csv.reader(open(sys.argv[1]), delimiter=\' \')

I`m importing a CSV containing data from year 2002 to 2011 , using this :

pair = csv.reader(open(sys.argv[1]), delimiter=' ')

names = [] ; date = [] ; open = [] ; close = [] ; min = [] ; max = []

#Parse the CSV file into a list
for data in pair:
开发者_StackOverflow    names.append(data[0])
names.pop(0)

How would I just keep the 2010 values ? ( from the date column ... )


You test on the date, and keep it only if it's the right year. How this is done depends on what format the date is in, etc. For example:

from datetime import datetime

for data in pair:
    date = datetime.strptime(data[1], <yourdateformat>)
    if date.year == 2010:
        names.append(data[0])


Assuming date is in '%d/%m/%Y' format

names = [data[0] for data in pair if data[1].endswith('2010')]
0

精彩评论

暂无评论...
验证码 换一张
取 消