1

I'd like to turn the following string into an array with 3 columns and multiple rows if possible in python. I'm attempting to turn the following into a workable numerical data set - using an array, and currently I'm unable to figure out how to turn it from a string --> array. I'm seeing that it's nearly in the format that I'd like it in. Is there a way to turn everything in between each apostrophe (ex. '-1,38,31857' as one row, and '-1,40,39304') as another row, and compile all of those into an array?

 '-1,38,31857',
 '-1,40,39304',
 '1582871,64,445338',
 '-1,37,29632',
 '-1,1,82',
 '-1,18,3613',
 '-1,6,544',
 '-1,23,7025',
 '-1,34,20775',
 '1979527,23,6361',
 '-1,10,1330',
 '-1,17,3300',
 '-1,11,1426',
 '-1,8,853',
 '-1,24,7087',
 '-1,1,0',
 '-1,1,0',
 '198113,79,1927770',
 '-1,1,0',
 '1763114,1,42',
 '1803615,4,357',
 '-1,1,0',
 '-1,1,0',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1',
 '-1,-1'] ```

3
  • 2
    What is the source of this string? this looks like you just split up the contents of some text file by line into a list. In any case, you should just parse this with the built-in csv module. Commented Feb 8, 2021 at 2:08
  • Also, when you say array, do you mean a list? Would just an iterator do? or do you mean a numpy array? Commented Feb 8, 2021 at 2:12
  • The source of the string is from the hiscores of a game called old school runescape which outputs as a string - and in order to be processed requires conversion into a proper array :) Example of the link that gives out the data set: services.runescape.com/m=hiscore_oldschool/… Commented Feb 8, 2021 at 2:27

1 Answer 1

2

Using list comprehension and split():

>>> [[int(n) for n in row.split(",")] for row in strings]
[[-1, 38, 31857], [-1, 40, 39304], [1582871, 64, 445338], [-1, 37, 29632], [-1, 1, 82], [-1, 18, 3613], [-1, 6, 544], [-1, 23, 7025], [-1, 34, 20775], [1979527, 23, 6361], [-1, 10, 1330], [-1, 17, 3300], [-1, 11, 1426], [-1, 8, 853], [-1, 24, 7087], [-1, 1, 0], [-1, 1, 0], [198113, 79, 1927770], [-1, 1, 0], [1763114, 1, 42], [1803615, 4, 357], [-1, 1, 0], [-1, 1, 0], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1]]

Another option is to use the csv module:

>>> import csv
>>> list(csv.reader(strings, quoting=csv.QUOTE_NONNUMERIC))
[[-1.0, 38.0, 31857.0], [-1.0, 40.0, 39304.0], [1582871.0, 64.0, 445338.0], [-1.0, 37.0, 29632.0], [-1.0, 1.0, 82.0], [-1.0, 18.0, 3613.0], [-1.0, 6.0, 544.0], [-1.0, 23.0, 7025.0], [-1.0, 34.0, 20775.0], [1979527.0, 23.0, 6361.0], [-1.0, 10.0, 1330.0], [-1.0, 17.0, 3300.0], [-1.0, 11.0, 1426.0], [-1.0, 8.0, 853.0], [-1.0, 24.0, 7087.0], [-1.0, 1.0, 0.0], [-1.0, 1.0, 0.0], [198113.0, 79.0, 1927770.0], [-1.0, 1.0, 0.0], [1763114.0, 1.0, 42.0], [1803615.0, 4.0, 357.0], [-1.0, 1.0, 0.0], [-1.0, 1.0, 0.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0], [-1.0, -1.0]]
Sign up to request clarification or add additional context in comments.

1 Comment

quoting argument is solid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.