0

I'm trying to get the top entry (string) in a matrix of data to be the variable name for the rest of the (numerical) data in each column. I've used the following to open the file and create the matrix.

with open('x.dat', 'r') as f:
    row = 0
    for line in f:
        words = line.split(',')
        for col in range(len(words)):
            DataMatrix[row][col] = words[col] 
        row += 1
f.close()

However, i can't see how to take the string and have it be recognized as a variable name for the "list" of data which will be filled by the column of numerics. This has got to be simpler than I'm making it. Any help?

The data file looks like: ... (can't seem to get the format to show correctly, but each [] is a row and the rows are stacked on top of one another)
['% Time','FrameNo','Version','X','Y','Z',…] ['66266.265514','948780','2.5','64','0','30'…] [66266.298785','948785', 2.5','63','0','32',…] …

3
  • 2
    I don't quite understand your question. Can you give us some example data and example output? Commented Dec 18, 2012 at 17:38
  • Do you mean that the first row of data is a list of column names which you want it to become the name of list variable in your script consisting of all the values from that column in the rest of the rows? Commented Dec 18, 2012 at 18:47
  • martineau - Exactly! The first row of the data file has string entries that I want to have become the variable name of a list that will consist of all the entries in their column. Commented Dec 18, 2012 at 20:22

2 Answers 2

1

What you are looking for is the vars built-in function of python. This will give you a dict representing the variables in scope.

I don't follow the code in your example enough to add this solution into it, but here is an example using vars that might help:

# Set up some data (which would actually be read from a file in your example)
headers = ['item', 'quantity', 'cost']
data = [['dress', 'purse', 'puppy'], [1, 2, 15], [27.00, 15.00, 2.00]]

for i in range(len(headers)):
  name = headers[i]
  value = list()
  for data_item in data[i]:
    value.append(data_item)
  # This sets the name of the header to the name of a variable
  vars()[name] = value

# Print to prove that vars() worked
print 'Items', item
print 'Quantities', quantity
print 'Costs', cost

Which produces the following output:

Items ['dress', 'purse', 'puppy']
Quantities [1, 2, 15]
Costs [27.0, 15.0, 2.0]
Sign up to request clarification or add additional context in comments.

Comments

0

Use the int function

with open('x.dat', 'r') as f:
    row = 0
    for line in f:
        words = line.split(',')
        for col in range(len(words)):
            DataMatrix[row][int(col)] = words[int(col)] 
        row += 1
f.close()

Alternatively, you could do a CSV reader to make this marginally easier.

with open('x.dat', 'rb') as csvfile:
    theReader = csv.reader(csvfile, delimiter=',')
    row=0;
    for line in theReader:
        row+=1
        for col in range(len(line)):
             DataMatrix[row][int(col)] = words[int(col)] 

6 Comments

delimiter=',' is redundant - and at least use enumerate(theReader, start=1) ? (but until the OP clarifies - we've no idea if this is correct or not!)
True enough, but I'm inclined to give the delimiter as an option until the OP gives an idea.
I'm going for the line.split(',') as a give-away?
I'm new to Python - not sure if I get this, but I the "col" seems to already be an integer since it's in the "range(len(words))". So using "int" does not seem to change the results. The code I posted works,... I just want additional code to handle the DataMatrix entries so I can use one of its entries as a variable name for the remainder of values in the column of the matrix.
@Mark: That is true enough, you don't need to use int(col), but at the very least, it would need to be done to use it as an index to the array. You might also look into dictionaries.
|

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.