gpt4 book ai didi

python - 如何使用具有逗号分隔符和空格的 pandas 解析 csv?

转载 作者:行者123 更新时间:2023-11-28 19:04:05 26 4
gpt4 key购买 nike

我目前有以下 data.csv,它有一个逗号分隔符:

name,day
Chicken Sandwich,Wednesday
Pesto Pasta,Thursday
Lettuce, Tomato & Onion Sandwich,Friday
Lettuce, Tomato & Onion Pita,Friday
Soup,Saturday

解析器脚本是:

import pandas as pd


df = pd.read_csv('data.csv', delimiter=',', error_bad_lines=False, index_col=False)
print(df.head(5))

输出是:

Skipping line 4: expected 2 fields, saw 3
Skipping line 5: expected 2 fields, saw 3

name day
0 Chicken Sandwich Wednesday
1 Pesto Pasta Thursday
2 Soup Saturday

我如何处理 Lettuce, Tomato & Onion Sandwich 这个案例。每个项目都应该用 , 分隔,但项目中可能有一个逗号后跟一个空格。期望的输出是:

                               name        day
0 Chicken Sandwich Wednesday
1 Pesto Pasta Thursday
2 Lettuce, Tomato & Onion Sandwich Friday
3 Lettuce, Tomato & Onion Pita Friday
4 Soup Saturday

最佳答案

在其他情况下也适用的替代方案。好吧,它很丑。

import pandas as pd
from io import StringIO

for_pd = StringIO()
with open('theirry.csv') as input:
for line in input:
line = line.rstrip().replace(', ', '|||').replace(',', '```').replace('|||', ', ').replace('```', '|')
print (line, file=for_pd)
for_pd.seek(0)

df = pd.read_csv(for_pd, sep='|')

print (df)

结果:

                               name        day
0 Chicken Sandwich Wednesday
1 Pesto Pasta Thursday
2 Lettuce, Tomato & Onion Sandwich Friday
3 Lettuce, Tomato & Onion Pita Friday
4 Soup Saturday

关于python - 如何使用具有逗号分隔符和空格的 pandas 解析 csv?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49013948/

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