简体   繁体   中英

flatten list of list of strings in python

I have the following list in python:

ipdb> xgboost_prediction
'[["2019-08-16 14:22:00.0", "ls5923_splunk", "False"]]'

and my goal is to append it to the csv file, which will happen on minute base.

Now, I thought I would convert is easily to pandas dataframe and append csv. But somehow I am struggling with it. How would I flatten it and convert to dataframe?

I tried:

ipdb> 
ipdb> list2 = [inner for item in xgboost_prediction for inner in ast.literal_eval(item)]
*** SyntaxError: unexpected EOF while parsing

ipdb> new_lst = [sub_val for val in xgboost_prediction for sub_val in eval(val)]
*** SyntaxError: unexpected EOF while parsing

Please advice.

Since you just want to append normally formatted values to a csv using pandas is overkill. You can simply convert your array to a comma separated string and append that to the file directly. If you think your string might have special characters you can also use the csv library which will automatically add the proper formatting to escape any strings.

xgboost_prediction = [["2019-08-16 14:22:00.0", "ls5923_splunk", "False"]]

with open('outputfile.csv', 'a+') as f:
    # Incase you are appending multiple predictions
    for entry in xgboost_prediction:
        f.write(','.join(entry))
    f.write('\n')

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