简体   繁体   English

将逗号分隔的值转换为Dict

[英]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. 现在,我想将其转换为Dict,以便可以使用cursor.executemany将其加载到数据库中,从而可以将dict作为输入。

Is there a module to auto convert this into a Dict. 是否有模块将其自动转换为Dict。 I looked at Numpy - loadtext but that requires me to write this first to a file. 我查看了Numpy-loadtext,但这需要我首先将其写入文件。 Is there a way that i can do this without creating a file? 有没有一种方法可以在不创建文件的情况下执行此操作?

Use csv.DictReader 使用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. 我查看了Numpy-loadtext,但这需要我首先将其写入文件。 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. 如果您不想拥有物理数据,则可以使用类似object的文件。

  • Use tempfile.TemporaryFile 使用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 使用io.StringIO

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pandas:来自dict的数据帧,以逗号分隔的值 - pandas: dataframe from dict with comma separated values 将数据转换为逗号分隔值 - Convert data into comma separated values 使用python将逗号分隔的值转换为逗号分隔的值 - convert comma separated values to comma delimited values using python 将python字典转换为逗号分隔的键字符串和逗号分隔值字符串的优雅方法是什么? - What is an elegant way to convert a python dictionary into a comma separated keys string and a comma separated values string? 将逗号分隔的键值对字符串转换为字典 - Convert a comma separated string of key values pairs to dictionary 在python中将空格分隔文件转换为逗号分隔值文件 - Convert a space delimited file to comma separated values file in python 如何在 python 的列表中转换带有逗号分隔值的字符串? - how to convert a string with comma separated values within a list in python? 如何将包含未用逗号分隔的值列表的字符串转换为列表? - How to convert a string containing a list of values that are not comma-separated to a list? Python pandas将逗号分隔值列表转换为dataframe - Python pandas convert list of comma separated values to dataframe 将 pandas 数据框列值转换为逗号分隔的字符串 - convert pandas data frame column values into comma separated strings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM