简体   繁体   中英

Getting syntax error while cleaning data using ast.literal_eval()

I have extracted data set from librosa library.

This is top five data.

These are the total columns.

These data are in object format.

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 code is working but it gives syntax error in most cases.

This is an error occurring in most cases.


After this I want to save each cleaned data to csv file in the same row and column format. So any suggestion for it too.

Don't replace all the spaces but only when they are inbetween 2 digits:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM