如何将a的str表示形式(dict如下面的字符串)转换为dict?
s = "{'muffin' : 'lolz', 'foo' : 'kitty'}"
Run Code Online (Sandbox Code Playgroud)
我不喜欢用eval.我还能用什么?
这样做的主要原因是我写的一个同事课程,将所有输入转换为字符串.我没心情去修改他的课程来处理这个问题.
尝试使用以下格式将csv文件读入pandas数据帧
dp = pd.read_csv('products.csv', header = 0, dtype = {'name': str,'review': str,
'rating': int,'word_count': dict}, engine = 'c')
print dp.shape
for col in dp.columns:
print 'column', col,':', type(col[0])
print type(dp['rating'][0])
dp.head(3)
Run Code Online (Sandbox Code Playgroud)
这是输出:
(183531, 4)
column name : <type 'str'>
column review : <type 'str'>
column rating : <type 'str'>
column word_count : <type 'str'>
<type 'numpy.int64'>
Run Code Online (Sandbox Code Playgroud)
我可以理解,大熊猫可能会发现很难将字典的字符串表示转换为字典并给出这个和这个.但是如何将"rating"列的内容同时为str和numpy.int64 ???
顺便说一下,不指定引擎或标题的调整不会改变任何东西.
感谢致敬