I am trying to take a CSV input file consisting of a single column of strings and convert it to an array in order to run some operations on each string separately. However, once I import the CSV, the resulting array is not structured as I had expected. Here is a code snippet that illustrates my problem:
import csv
regular_array = ["this", "is", "a", "test"]
# Import a csv with the same elements as regular_array in separate rows
# Put it in an array
csv_doc = csv.reader(open('tester.csv', 'rb'), delimiter=",", quotechar='|')
csv_array = []
for row in csv_doc:
csv_array.append(row)
# Let's see if we can pass an element from these arrays into a function
print len(regular_array[0]), "\n", len(csv_array[0])
# Well that doesn't do what I thought it would
# Let's print out the arrays and see why.
print regular_array[0], "\n", csv_array[0]
# AHA! My arrays have different structures.
As you might expect, I get different results for both operations due to the structure of the arrays. The first array consists of letters and therefore len(regular_array[0]) = 4. The second is consists of elements and len(csv_array[0]) = 1.
For my purposes, I need my arrays to be of the first kind.
My question has two parts: 1) Can someone point me some resources to help me understand what phenomenon I am dealing with? (Not entirely comfortable with the differences between list/array/tuple constructs yet)
2) Is there an approach I can use to convert my CSV input into the first kind of array, or is there an better way to go about storing the data once it is imported?
Thanks in advance.