Im pretty new to Python, so i apologise in advance.
My main goal is to parse a csv file (as per below) and to create a list named based on column 5 which includes values from column 7 as the elements.

01/11/2010,DEB,12345,12345,CREDIT CARD,,44
01/11/2010,DEB,12345,12345,TAKE AWAY,,20
01/11/2010,DEB,12345,12345,TAKE AWAY,,22
01/11/2010,DEB,12345,12345,TAKE AWAY,,24

The code I have so far is :

#!/usr/bin/python

import csv
import string

itemlist = []

reader = csv.reader(open("statement.csv", "rb"))
for row in reader:
        itemlist.append(row[4])
        itemlist = list(set(itemlist))

for x in itemlist[:]:
        item = x
        item = []

The bit im getting a bit lost on is how to create a list from each element of my itemlist list.

Thanks in advance...

column 5 has words like TAKE AWAY and CREDIT CARD.

I have not used csv lot but would you not need to set separator to comma. If the reader part works, you end up with unique items in column 5 of file in itemlist, but you need to do line 11 only once after loop not inside loop. This is how it looks to me by only reading the code, I may be wrong...

Line 15 undos line 14.,

thanks for your quick response, the reade section seems to work fine but it is the assign the itemlist elements to new lists and then appending further elements from the csv to it that I am having issues with ......

You have saved only column 5, you have no further elements.

#!/usr/bin/python

import csv
itemlist = []

reader = csv.reader(open("statement.csv", "rb"))
for row in reader:
    itemlist.append(row[4])

# only one time!
itemlist = list(set(itemlist))

print itemlist

""" Output:
['CREDIT CARD', 'TAKE AWAY']
"""

ok thanks. The itemlist is fine but ideally i wanted to
** create additonal lists based on each of the itemlist elements.
** With these new lists assign the column 7 vaules from the csv to the relevant line.

My overall plan is to have a list named "TAKE AWAY" with a number of vaules as elements that I can add together...

Thanks for all your help....

Ive managed get this working in the end using a dictonary....

#!/usr/bin/python

import csv
import string

inputFile = open(str("statement.csv"),  'r')

itemlist = []
outputDic = {}
keyIndex = 0

fileReader = csv.reader(inputFile)
for line in fileReader:
        keyIndex=line[4]
        if len(line[6]) == 0:
                pass
        elif outputDic.has_key(keyIndex) == True:
                oldKey = outputDic[keyIndex]
                newKey = line[6]
                newKey=float(newKey)
                oldKey=float(oldKey)
                keySum = newKey+oldKey
                keySum2 = round(keySum, 2)
                outputDic[keyIndex] = str(keySum2)
        else:
                outputDic[keyIndex] = line[6]

print outputDic
elif outputDic.has_key(keyIndex) == True:

No need for True keyword.

You can do this without the keyword True as If statement defaults to true.
simply...

elif outputDic.has_key(keyIndex):

Also helps ;)

Ive managed get this working in the end using a dictonary....

#!/usr/bin/python

import csv
import string

inputFile = open(str("statement.csv"),  'r')

itemlist = []
outputDic = {}
keyIndex = 0

fileReader = csv.reader(inputFile)
for line in fileReader:
        keyIndex=line[4]
        if len(line[6]) == 0:
                pass
        elif outputDic.has_key(keyIndex) == True:
                oldKey = outputDic[keyIndex]
                newKey = line[6]
                newKey=float(newKey)
                oldKey=float(oldKey)
                keySum = newKey+oldKey
                keySum2 = round(keySum, 2)
                outputDic[keyIndex] = str(keySum2)
        else:
                outputDic[keyIndex] = line[6]

print outputDic

I simplified your code little for you, if you want to print rounded figures later, use print("%.2f" % value) in printing them.

#!/usr/bin/python
import csv

inputFile = open(str("statement.csv"),  'r')
outputDic = {}

fileReader = csv.reader(inputFile)
for line in fileReader:
        keyIndex=line[4]
        if line[6]:
            if outputDic.has_key(keyIndex):
                    outputDic[keyIndex] = outputDic[keyIndex] + float(line[6])
            else:
                    outputDic[keyIndex] = float(line[6])

print outputDic
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.