0

I'm attempting to import a csv file that has a many inputs of text strings followed by several values. Something like:

FruitBat, 20, 46, 12, 45. RocketTurtle, 45, 22, 17, 90

I would like to be able to import this csv such that it automatically generates lists named FruitBat and RocketTurtle such that:

FruitBat = [20, 46, 12, 45], RocketTurtle=[45, 22, 17, 90], etc.

The actual file I wish to import is several hundred inputs. Is there any good way to do this? Thanks.

Caleb H.
  • 1,657
  • 1
  • 10
  • 31
  • Have you already written code to read the csv file, or are you making that part of the question? – Caleb H. May 14 '19 at 23:11
  • I can import a csv file and its data fine, the issue is getting the code to generate list names based on the imported strings. – PhoenixM May 14 '19 at 23:18
  • Please provide a sample of input data. – gmds May 14 '19 at 23:21
  • https://imgur.com/a/UTF9QmU here's a bit of the csv I'm working with. I would need it to output something like cs137=[ 109999996.147162, 59041804.3408036, 29573360.5568435, 23486287.013845, 18652113.5004746, 14812956.0806909, 11764010.9708154, 9342629.07198266, 7419639.28741607, 5892457.75800508] – PhoenixM May 14 '19 at 23:26
  • I updated my answer to work with your example string. – Caleb H. May 14 '19 at 23:38

2 Answers2

0

Unless there is specific need for the variables to be made seperately, it would be easier to just store these as key-value pairs inside a dictionary.

You could read the files and use the first element as the key and slice the rest of the elements into a list.

import csv

# dictionary to hold all the lists
output_dict= {}

with open('file.csv', 'r') as f:
    reader = csv.reader(f)
    for row in reader:
        # store key-value pairs
        output_dict[row[0]] = row[1:]

# get the corresponding values
print(output_dict["FruitBat"])

If you really want to create seperate variables, might be worth looking at globals()

razdi
  • 1,388
  • 15
  • 21
0

You can split the strings by commas, and then make a dictionary with the data:

import re

arr = yourString.split(",")

players = {} #I'm calling them players; I don't really know what they are

currentPlayer = arr[0]

players[currentPlayer] = []

for i in range( len(arr) ):
    if not re.search('[A-Za-z]', arr[i]): #If there are no letters in it
        players[currentPlayer].append(arr[i]) #add it to that player
    else: #If there were letters in it
        currentPlayer = arr[i]
        players[currentPlayer] = []

print(players)

I hope this helps!

EDIT: I didn't realize your 'players' data was separated by periods. In that case, you could do a similar thing like this:


import re

players_arr = yourString.split(".")

players = {}

for i in range( len(players_arr) ):
    data = players_arr[i].split(",")
    player = ""
    for j in range( len(data) ):
        if not re.search('[A-Za-z]', data[j]):
            players[player].append(data[j])
        else:
            player = data[j].strip()
            players[player] = []

print(players)

Caleb H.
  • 1,657
  • 1
  • 10
  • 31