python - 带正则表达式的 read_csv
问题描述
csv 数据示例:
<pre>2019-08-15 00:00:06,430 0:0 - {"info":{"name":"LTD - PUBLIC"}}</pre>
<pre>pd.read_csv(filepath, sep= ' - ', header=None, engine='python')</pre>
预期的:
<pre>
date info
2019-08-15 00:00:06,430 0:0 {"info":{"name":"LTD - PUBLIC"}}
</pre>
错误信息:
ParserError: Expected 2 fields in line 1, saw 3. 错误可能是由于使用多字符分隔符时忽略引号引起的。
解决方案
使用regex
sep
temp = StringIO("""
2019-08-15 00:00:06,430 0:0 - {"info":{"name":"LTD - PUBLIC"}}
""")
df = pd.read_csv(temp, sep=r' - (?={)', engine='python',header=None)
df.rename({0:'date',1:'info'},axis=1)
输出
date info
0 2019-08-15 00:00:06,430 0:0 {"info":{"name":"LTD - PUBLIC"}}
推荐阅读
- angular - 使用 Chart.js 和 ng2-charts 格式化图表(角度)
- flutter - setState 重建父小部件,导致 FutureBuilders 中的闪烁效果(因为它们重建?)
- python - 井字游戏功能未正确定义变量
- node.js - 安装我的包时如何复制 JSON 配置文件
- javascript - 数组过滤器 - 寻找带有字母和数字的字符串值
- c# - 在 c# 中哪里设置 Context.Request.QueryString 或 Context.Request.Param?
- java - 如何在 Intellij 中按优先级运行 testng 测试?
- c++ - 生成一个使用协议缓冲区的 DLL
- javascript - 浏览器的本地存储不断重置为未定义
- mongodb - 在 MongoDB 中,子文档实际上是单独的 BSON 文档吗?