0

I have a dictionary like this:

data_heading = {'Date': [(3,2)],
                'Rate': [(3,3)],
                'Product': [(3,7)]}

the KEY defines the heading of column in csv file, the VALUE defines the [(row number, col number)]
I want to read the data, starting from a specific LINE, and specific COLUMN, till the end of csv file.
After reading the contents of file I want to write a new csv of the read data. I am much confused in this problem, I have tried a long long code to do so, but still unable to write such file.
I'll be Thankful for your kind support. Please help me.
I have read the data and stored it in a list like:

data = ['Date', '2013-03-01', '2013-03-01', '2013-03-01', 'Rate', '$ 50', '$ 60', '$ 70', 'Product', 'MOBILE', 'DVD', 'TV']

but i am unable to write it in a csv. . .

2
  • Are you aware of the csv module? Commented Mar 1, 2013 at 11:30
  • 1
    yeah I Know how to read and write a csv but to fetch the DESIRED data from a csv seems much difficult to me :( Commented Mar 1, 2013 at 11:31

1 Answer 1

1

How about this (untested since you didn't provide full code+data):

import csv

data_heading = {'Date': [(3,2)],
            'Rate': [(3,3)],
            'Product': [(3,7)]}

rdr = csv.reader(open(csvpath, 'rb'))
columns = [{key:row[pos[0][1]] for key,pos in data_heading.items()} for row in rdr]
# finally trim to desired row startpoints:
data = {key:[col[key] for col in columns[pos[0][0]:]] for key,pos in  data_heading.items()}
print data

It's a little gnarly because your data_heading is in a slightly awkward format. If you have freedom to change that, the code can be made more readable.

You can do the CSV writing after that - data should be a simple enough dict to write out.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks the data now seems much clearer :) now I hope to find a simple way to write it in a csv :) and thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.