By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,171 Members | 776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,171 IT Pros & Developers. It's quick & easy.

csv to array

P: 13
hi,I am new to python.
my csv file contains data with combination of strings and integers.if i try to store it in a array using array module,type code is not matching.
as csv reader returns list of lists,wat type code i have to give??

a=array.array('typecode',[initalizer])

wats the solution for it?
Nov 21 '07 #1
Share this Question
Share on Google+
1 Reply


bartonc
Expert 5K+
P: 6,596
hi,I am new to python.
my csv file contains data with combination of strings and integers.if i try to store it in a array using array module,type code is not matching.
as csv reader returns list of lists,wat type code i have to give??

a=array.array('typecode',[initalizer])

wats the solution for it?
We don't typically use arrays in Python, except when that's truly what we need.

a Python list acts much like an array but may contain any type of data in a given element. Arrays are for a single type of data, hence the 'typecode'.

A cvs reader will give you a 2D list which would look something like this:
Expand|Select|Wrap|Line Numbers
  1. >>> cvsString = "1, hello, world\n2, goodnight, all"
  2. >>> a2dList = [[item.strip() for item in line.split(',')] for line in cvsString.split('\n')]
  3. >>> a2dList[0]
  4. ['1', 'hello', 'world']
  5. >>> a2dList[0][2]
  6. 'world'
  7. >>> 
Nov 21 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.