0

enter image description hereMy text file is a Python list.

I'm trying to read this into a python list and this is what I have

with open('C:\\Users\\Moondra\Anaconda_related\Jupyer_notebooks\MachineLearning\Small_cap_Bio.txt', 'r') as f:
    lines = f.read().split('\n')
    bio = [ i.replace('"',"").replace("'","").replace('[', '').replace(']', '').replace(',','').lstrip().rstrip() for i in lines]

Besides using regular expressions, is there a cleaner(easier to understand) way to write this code?

Thank you.

Moondra
  • 4,399
  • 9
  • 46
  • 104

1 Answers1

2

If its a python list, then use python

import ast
with open('C:\\Users\\Moondra\Anaconda_related\Jupyer_notebooks\MachineLearning\Small_cap_Bio.txt', 'r') as f:
    my_list = ast.literal_eval(f.read())
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • Yeah, it's a python list. However, ran into this ValueError: `ValueError: malformed node or string: <_ast.Name object at 0x0000000005403748>`. I wonder if it's because I have some nan values in my list. – Moondra May 04 '17 at 23:54
  • Hmm..., can you post an example with the `nan`? `nan` isn't a python thing, `None` is. It sounds like this isn't python after all. How did it get created? – tdelaney May 04 '17 at 23:55
  • I will take a screenshot and post it. I think I also may have also used Pandas. I scraped a few times, and I think I used `Pd.read_html() `for one of them. – Moondra May 04 '17 at 23:57
  • Actually, `float('nan')` is a thing. Maybe that's it. If this came from, say, `print(some_list)` then you've got problems. – tdelaney May 04 '17 at 23:57
  • I've uploaded it. – Moondra May 05 '17 at 00:02
  • Do you control the software that saved the data? How did you do that? You may want to choose a different serialization like json, or pickle. – tdelaney May 05 '17 at 00:03
  • Originally, I had stored it using the magic command `%store` on jupyter notebook(which seems to be similar to pickle?), but after restarting the server, I couldn't reload the list via '%store -r' , so I had to rely on a back-up txt.document. It's okay though. I was mainly curious if there was a less messy code that what I presented. Thank you for your help. – Moondra May 05 '17 at 00:12
  • You could do `ast.literal_eval(f.read().replace('\nnan,', '\nNone,'))`. Its hackish, but if its just to get you out of a jam... – tdelaney May 05 '17 at 00:16