-2

I have:

"[15765,22832,15289,15016,15017]"

I want:

[15765,22832,15289,15016,15017]

What should I do to convert this string to list?

P.S. Post was edited without my permission and it lost important part. The type of line that looks like list is 'bytes'. This is not string.

P.S. №2. My initial code was:

import urllib.request, re
f = urllib.request.urlopen("http://www.finam.ru/cache/icharts/icharts.js")
lines = f.readlines()

for line in lines:
    m = re.match('var\s+(\w+)\s*=\s*\[\\s*(.+)\s*\]\;',  line.decode('windows-1251'))
    if m is not None:
        varname = m.group(1)
        if varname == "aEmitentIds":
            aEmitentIds = line #its type is 'bytes', not 'string'

I need to get list from line

line from web page looks like

[15765, 22832, 15289, 15016, 15017]
DeepSpace
  • 153
  • 1
  • 2
  • 11
  • 6
    You can import json and do good_list = json.loads(your_string_list) – Wonka Jul 10 '19 at 10:36
  • 3
    What @Wonka wrote and `ast.literal_eval` are among the best options. Do **not** use `eval` or `exec` – DeepSpace Jul 10 '19 at 10:41
  • You can do it just using split function, for details check my answer below. – Safak Ozdek Jul 10 '19 at 10:53
  • 1
    Post was edited without my permission and it lost important part. The type of line that looks like list is 'bytes'. This is not string. – DeepSpace Jul 10 '19 at 10:56
  • 1
    Approving an edit is explicitly giving them your permission. Suggested edits that make incorrect changes can and should be rejected. – jpmc26 Jul 12 '19 at 21:46

3 Answers3

4

Assuming s is your string, you can just use split and then cast each number to integer:

s = [int(number) for number in s[1:-1].split(',')]

For detailed information about split function: Python3 split documentation

Safak Ozdek
  • 906
  • 11
  • 18
  • 1
    this return string not integers. You should use this `[int(i) for i in a[1:-1].split(',')]` ` – Lucas Wieloch Jul 10 '19 at 10:40
  • Post was edited without my permission and it lost important part. The type of line that looks like list is 'bytes'. This is not string. – DeepSpace Jul 10 '19 at 10:56
  • In the question it says string like everywhere. Even the name of this question is asking how to convert string to list. Maybe you can just upvote the best answer that suits edited question and create a new question that explains your point clearly. @DeepSpace – Safak Ozdek Jul 10 '19 at 11:02
  • @DeepSpace Can you please edit your question and insert the exact type of what you want to convert? (please also change the title if that is the case) – Lucas Wieloch Jul 10 '19 at 11:13
  • @AlicanŞafakÖzdek Yes, I think you are correct, thanks for the heads up. – Lucas Wieloch Jul 10 '19 at 11:27
3

What you have is a stringified list. You could use a json parser to parse that information into the corresponding list

import json
test_str = "[15765,22832,15289,15016,15017]"
l = json.loads(test_str)  # List that you need.

Or another way to do this would be to use ast

import ast
test_str = "[15765,22832,15289,15016,15017]"
data = ast.literal_eval(test_str)

The result is

[15765, 22832, 15289, 15016, 15017]

To understand why using eval() is bad practice you could refer to this answer

Sudheesh Singanamalla
  • 2,283
  • 3
  • 19
  • 36
  • The input file is a JS file. Parsing as JSON would fail if it included `Infinity` or a calculation. These would also likely fail if a computation or variable is included in the list as well. – jpmc26 Jul 12 '19 at 21:51
2

You can also use regex to pull out numeric values from the string as follows:

import re 
lst = "[15765,22832,15289,15016,15017]"
lst = [int(number) for number in re.findall('\d+',lst)]

Output of the above code is,

[15765, 22832, 15289, 15016, 15017]
ajithb
  • 36
  • 4