gpt4 book ai didi

python - 使用ast.literal_eval()清理数据时出现语法错误

转载 作者:行者123 更新时间:2023-12-03 08:26:13 25 4
gpt4 key购买 nike

我已经从librosa库中提取了数据集。

This is top five data.

These are the total columns.

这些数据为对象格式。

It has got '\n' and spaces and all. So, it is needed to be cleaned

import ast
import numpy as np
import re

# removing \n and converting into number format
if '\n' in df['MFCC_1'][0]:
row = df['MFCC_1'][0]
row = ast.literal_eval(re.sub('\s+', ",", df['MFCC_1'][0].replace('\n', '')))

print(row)

该代码有效,但在大多数情况下会给出语法错误。

This is an error occurring in most cases.

之后,我想以相同的行和列格式将每个清理的数据保存到csv文件中。所以对此也有任何建议。

最佳答案

不要替换所有空格,仅当它们在两位数之间时才替换:

if '\n' in df['MFCC_1'][0]:
row = df['MFCC_1'][0].replace('\n', '')
row = ast.literal_eval(re.sub(r'(?<=\d)\s+(?=\d)', ",", row))

关于python - 使用ast.literal_eval()清理数据时出现语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55285441/

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