简体   繁体   中英

How to convert string including an array into an array in python?

I am working on an assignment of bioinfo.

I have converted from SMILES to the fingerprints(fp) for a group of molecules, and the data frame looks like this: 在此处输入图像描述

The type of fingerprint is:
在此处输入图像描述

Then save the file to CSV file with to_csv .

Then reading the file with pd.read_csv , the fingerprint changes into a string and looks like: 在此处输入图像描述

I replace the \n with empty space, 在此处输入图像描述 , but the type is still a string.

I have used various methods:

  1. ast.literal_eval(fp_df['fp']) , then there is error: ValueError: malformed node or string: , from the most upvoted answer here
  2. list(fp_df['fp']) can't change the type of string
  3. [n.strip() for n in x] doesn't work: 项目清单

And other methods have also been tried.

May I ask for your help on how to deal with it? Thanks in advance.

If your value looks like:

"[1 0 0 1 1 0]"

Then you can turn it into a list by:

values = value.lstrip("[").rstrip("]").split()

But the values will still be numeric strings, so you could cast to int by:

values = [int(n) for n in value.lstrip("[").rstrip("]").split()]

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