python - 在python中拆分键值字符串并将其移动到df列中

标签 python regex pandas split

这是我拥有的列,我想将其拆分为键值并存储在 pandas df 中的新列中。

{"FontStyle"=>"Gill Sans Standard", "FontSize"=>"Medium (3mm)"}
{"Font Style"=>"Gill Sans Standard","Font Size"=>"Medium (3mm)"}
{"Font Style":"Script","Font Size":"Medium (3mm)"}
{"Font Style"=>"Gill Sans Standard","Font Size"=>"Medium (3mm)"}
{"Font Style":"Gill Sans Standard","Font Size":"Medium (3mm)"}

主要问题是有些有“=>”而有些有冒号

我想要 df 中的两个新列,一个用于字体样式,另一个用于字体大小以及其中受尊重的值

如果有人可以帮助我实现这一目标,那就太好了,如果您能给我推荐一些关于正则表达式的书籍/教程,那就太好了。

谢谢

最佳答案

这不是迄今为止最有效的代码,但它可以完成工作。

import pandas as pd
import ast

text = '''{"FontStyle"=>"Gill Sans Standard", "FontSize"=>"Medium (3mm)"}
{"Font Style"=>"Gill Sans Standard","Font Size"=>"Medium (3mm)"}
{"Font Style"=>"Script","Font Size"=>"Medium (3mm)"}
{"Font Style"=>"Gill Sans Standard","Font Size"=>"Medium (3mm)"}'''

my_list = []

text = text.replace("FontStyle", "Font Style")
text = text.replace("FontSize", "Font Size")
text = text.replace("=>", ":")
text = text.split("\n")

for one_dict in text:
    my_list.append(ast.literal_eval(one_dict))

df = pd.DataFrame(my_list)
print(df)

上述代码的输出:

      Font Size          Font Style
0  Medium (3mm)  Gill Sans Standard
1  Medium (3mm)  Gill Sans Standard
2  Medium (3mm)              Script
3  Medium (3mm)  Gill Sans Standard

我希望这有帮助。 :-) 如果确实如此,请告诉我。

关于python - 在python中拆分键值字符串并将其移动到df列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55156019/

相关文章:

arrays - Python Pandas : selecting 1st element in array in all cells

python - Pandas:针对时间序列数据生成直方图/数据透视图

python - 在 python 中使用 Pandas 将列附加到数据框

python - SQLAlchemy 参数错误 : ondelete undefined

python - 排序字典 python 值降序,但键升序

python - 无法在 Python 2.7.9 虚拟环境中导入 _winreg

python - Python 循环中的正则表达式

python - 查找没有 edge-networkx python 的节点对

java - 在 Java 中用多个连续逗号拆分字符串

python - 用于匹配某个内容直到某个点的正则表达式