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

时间:2019-03-21 16:48:57

标签: 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

Function Get_Params(ws As Worksheet, LR As Long, Target As Range) As Variant()

Dim Temp As Worksheet: Set Temp = ThisWorkbook.Sheets("Temp")
Dim LR2 As Long

ws.Range("D1:D" & LR).SpecialCells(xlCellTypeVisible).Copy
Temp.Range("U1").PasteSpecial xlPasteValues

Temp.Range("U1").RemoveDuplicates 1, xlYes
    LR2 = Temp.Range("U" & Temp.Rows.Count).End(xlUp).Row

    Get_Params = Temp.Range("U2:U" & LR2).Value '<--- ERROR HERE (LR2 = 2)

    Temp.Range("U1").EntireColumn.ClearContents

End Function

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

This is an error occurring in most cases.


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

1 个答案:

答案 0 :(得分:1)

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

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))