gpt4 book ai didi

python - 带有正则表达式的 read_csv

转载 作者:太空宇宙 更新时间:2023-11-04 11:13:42 26 4
gpt4 key购买 nike

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. Error could possibly be due to quotes being ignored when a multi-char delimiter is used.

最佳答案

使用正则表达式 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"}}

关于python - 带有正则表达式的 read_csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57551978/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com