myloginid
myloginid

Reputation: 1473

Convert Comma Separated Values to Dict

I have some text data

>>> print content
Date,Open,High,Low,Close,Volume,Adj Close
2015-03-17,4355.83,4384.98,4349.69,4375.63,1724370000,4375.63
2015-03-16,4338.29,4371.46,4327.27,4370.47,1713480000,4370.47
2015-03-13,4328.09,4347.87,4289.30,4314.90,1851410000,4314.90
2015-03-12,4302.73,4339.20,4300.87,4336.23,1855110000,4336.23
2015-03-11,4336.05,4342.87,4304.28,4305.38,1846020000,4305.38

Now I want to convert this into a Dict, so that I can load this into a database using the cursor.executemany that allows me to provide dict as an input.

Is there a module to auto convert this into a Dict. I looked at Numpy - loadtext but that requires me to write this first to a file. Is there a way that i can do this without creating a file?

Upvotes: 2

Views: 1364

Answers (1)

Sede
Sede

Reputation: 61225

Use csv.DictReader

>>> with open('text.txt') as f:
...     dreader = csv.DictReader(f)
...     for row in dreader:
...         print(row)
... 
{'Adj Close': '4375.63', 'High': '4384.98', 'Volume': '1724370000', 'Low': '4349.69', 'Close': '4375.63', 'Open': '4355.83', 'Date': '2015-03-17'}
{'Adj Close': '4370.47', 'High': '4371.46', 'Volume': '1713480000', 'Low': '4327.27', 'Close': '4370.47', 'Open': '4338.29', 'Date': '2015-03-16'}
{'Adj Close': '4314.90', 'High': '4347.87', 'Volume': '1851410000', 'Low': '4289.30', 'Close': '4314.90', 'Open': '4328.09', 'Date': '2015-03-13'}
{'Adj Close': '4336.23', 'High': '4339.20', 'Volume': '1855110000', 'Low': '4300.87', 'Close': '4336.23', 'Open': '4302.73', 'Date': '2015-03-12'}
{'Adj Close': '4305.38', 'High': '4342.87', 'Volume': '1846020000', 'Low': '4304.28', 'Close': '4305.38', 'Open': '4336.05', 'Date': '2015-03-11'}

I looked at Numpy - loadtext but that requires me to write this first to a file. Is there a way that I can do this without creating a file?

You can use a file like object if you do not want to have physical data.

  • Use tempfile.TemporaryFile

    from tempfile import TemporaryFile
    
    with TemporaryFile('w+t') as flike:
        flike.write(content)
        flike.seek(0)
        dreader = csv.DictReader(flike)
        for row in dreader:
            #do something
    
  • Use io.StringIO

    import io #python3 or import StringIO in python2
    flike = io.StringIO(content)
    for row in csv.DictReader(flike)
        #do something
    

Upvotes: 6

Related Questions