[Tutor] Converting a string into dictionary references
Don Jennings
dfjennings at gmail.com
Mon Apr 23 18:40:14 CEST 2012
Remember, the keys for the dictionary in your example are simply strings. It just so happens that those strings are in another file. Read the file which contains the specified columns and split on the comma into variable names for your output:
with open(spec_file, 'r') as f:
outcol1, outcol2, outcol3 = f.readline().split(',')
# use variable names to access dictionary
print row[outcol1] + ',' + row[outcol2] + ',' + row[outcol3]
Now, if you are unsure how many columns will be in the file with specified columns, that changes things slightly:
# get a list of the requested columns
outcols = []
with open(spec_file, 'r') as f:
line = f.readline()
outcols.extend(line.split(','))
# later, use that list to do something with the output (I assume strings for your data here)
print ','.join([row[outcol] for outcol in outcols])
Helpful?
Take care,
Don
> Not quite,
>
> I have csvfile1:
> column1, column2, column3, ... column200
>
> That is my raw data but I want to use only 5 columns for example in a
> specific application.
> I thus want a file with the following:
> column33,column1,column5
>
> I then want to read the original csv file and write a new csv file with the
> requested columns only.
>
> Does that make more sense?
>
> Regards
>
> On 23 April 2012 14:41, Joel Goldstick <joel.goldstick at gmail.com> wrote:
>
>> On Mon, Apr 23, 2012 at 8:56 AM, Gerhardus Geldenhuis
>> <gerhardus.geldenhuis at gmail.com> wrote:
>>> Hi
>>> Appologies about the subject I could not think of a better description.
>>>
>>> I have this very simple function:
>>>
>>> def readcsvfile(filename):
>>> f = open(filename, 'ro')
>>> csvdata = csv.DictReader(f)
>>> for row in csvdata:
>>> print row["column3"]+','+row["column1"]
>>>
>>> I have another inputfile that will be comma separated list of values.
>>> Eg:
>>> column3,column4,column10
>>>
>>> The idea is that I use this inputfile to tranform the original csv file.
>> I
>>> thus want a new file with only the specified columns. I am sure there is
>> an
>>> elegant way of doing this but I am not sure how to convert my print
>>> statement into something more dynamic. Any pointers would be appreciated.
>>>
>>> Regards
>>>
>>> --
>>> Gerhardus Geldenhuis
>>>
>>> _______________________________________________
>>> Tutor maillist - Tutor at python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>>
>> So you want to take 'column1' and get back 1?, 'column10' and get back 10?
>>
>> s = 'column1'
>> i = int(s[6:])
>>
>> This will only work if your strings all start with the text 'column'
>>
>> --
>> Joel Goldstick
>>
>
>
>
> --
> Gerhardus Geldenhuis
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://mail.python.org/pipermail/tutor/attachments/20120423/aa787769/attachment.html>
>
> ------------------------------
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> End of Tutor Digest, Vol 98, Issue 58
> *************************************
More information about the Tutor
mailing list