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

标签 python regex syntax-error data-cleaning mfcc

我已经从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/

相关文章:

python - 如何使用 OpenCV 捕获多个摄像头流?

java - 为什么零长度字符总是保留在 java 正则表达式模式 a 的源字符串的末尾?

regex - Ansible 正则表达式转义美元字符

python - 生成正则表达式列表以替换数据框中的单词列表python

c# - C#语法错误,似乎没有错误

python - 将带有参数的函数传递给函数时的语法错误(python)

python - GRU加载模型错误,ValueError : GRU(reset_after=False) is not compatible with GRU(reset_after=True)

python - 如何隐藏 QComboBox 项目而不是清除它们

python - 在 Python 中实现任意图像算法时处理边界像素的有效方法

sql - Mysql 语句不适用于多个 AND 子句