2017-08-26 13 views
-1

我是python的新手,试图拆分列表并将其存储为数组。将列表存储到python的数组中

我有一个csv文件,我正在读取并将每行存储为一个列表。这些行中的每一行都用逗号分隔,我希望将每个值存储到数组中

这是我当前检索csv文件中的行的代码。我卡在试图每一行存储解析到一个数组

def read_csv(self): 
    csvFileArray=[] 
    with open('myfile.csv','rb') as csvfile: 
     bufferreader=csv.reader(csvfile,delimiter=',') 
     for row in bufferreader: 
      csvFileArray.append(row) 
    return csvFileArray 

print(csvFileArray[0])输出将['11043299,1,2,1,true']。现在我想将这个输出作为数组轻松访问,以便进一步计算。举例来说 - 我应该打印true当我print(newArray[4])

+1

这里没有数组。仅列出 –

+0

解析CSV文件的方式有问题。你确定它是用逗号分隔的吗?请包含myfile.csv的第一行和匹配的'print(csvFileArray [0])'。 – DyZ

+2

'csv.reader()'应该已经将它分解成一个列表。 – AChampion

回答

0

我觉得你的方法可以为这样写:

def read_csv(self): 
     return list(open('myfile.csv')) 

def read_csv(self): 
     with open('myfile.csv') as f: 
      return list(f) 

如果你需要尽快关闭文件描述符因为等待因为Python GC会影响你的性能。看到我的评论。

为逗号分隔值的字符串转换为值的列表只是做:你看看什么Python标准库所提供的here

+1

'list(open('myfile.csv'))'坏,因为它不使用'with'来访问文件。 – DyZ

+0

@DYZ不需要。如果你不喜欢Python GC决定去做它的工作,或者当你不得不担心打开的文件在需要的时候耗费资源的时间太长。在大多数情况下,源代码简单性和性能影响很小,这是一种折衷。我选择这里是为了简单。不过,感谢您的评论,我会丰富我的回答。 –

0

comma_separeted.split(',') 

可能是有用的print(csvFileArray [0])的输出将是['11043299,1,2,1,true']。

我不这么认为;这是一个字符串列表,而不是五个元素例如。

$ echo '11043299,1,2,1,true' >> myfile.csv 

In [1]: import csv 

In [2]: with open('myfile.csv') as f: 
    ...:  reader = csv.reader(f) 
    ...:  rows = [row for row in reader] 
    ...: 

In [3]: rows 
Out[3]: [['11043299', '1', '2', '1', 'true']] 

难道这就是你想干什么?

In [4]: rows[0][4] 
Out[4]: 'true' 
+0

如果CSV文件有如下内容 - 1501043799,1,2,1,真正 1501044312,3,3,2,假 那么你csvFileArray的输出[0]会是什么,我提到 @ cricket_007 – Firstname

+0

不,它不会...继续看看'['11043299,1,2,1,true']!= ['11043299','1','2','1','true ']'用Python告诉你 –

0

查看CSV模块。 csv.DictReader类将CSV文件行放置到字典中,并将标题行字段作为字典键。但是请注意,CSV模块将数据作为字符串读取,因此您可能需要进行一些类型转换。

import csv 

with open('favorite_color.csv', 'rb') as csvfile: 
    reader = csv.DictReader(csvfile) 
    data = {} 
    for row in reader: 
     for field, value in row.iteritems(): 
      if not field in data: 
       data[field] = [value] 
      else: 
       data[field].append(value) 
data['correct_answers'] = [int(val) for val in data['correct_answers']] 
boolval = {'true': True, 'false': False} 
data['cross_bridge'] = [boolval[val.lower()] for val in data['cross_bridge'] 

它将CSV的内容很好地放入字典中。

In [2]: data 
{'correct_answers': [0, 3, 3, 2], 
'cross_bridge': [False, True, True, False], 
'favorite_color': ['orange', 'green', 'blue', 'yellow'], 
'first_name': ['Dirk', 'King', 'Lancelot', 'Sir'], 
'last_name': ['Diggler', 'Arthur', 'du Lac', 'Galahad']} 
In [3]: data['cross_bridge'][0] 
Out[3]: False 

或者,如果你真的想每一行是一个列表,你可以使用CSV。阅读()代替:

import csv 

with open('favorite_color.csv') as csvfile: 
    reader = csv.reader(csvfile) 
    data = [] 
    for row in reader: 
     data.append(row) 

,让你列表的列表:

In [5]: data 
Out[5]: 
[['first_name', 
    'last_name', 
    'favorite_color', 
    'correct_answers', 
    'cross_bridge'], 
['Dirk', 'Diggler', 'orange', '0', 'False'], 
['King', 'Arthur', 'green', '3', 'True'], 
['Lancelot', 'du Lac', 'blue', '3', 'True'], 
['Sir', 'Galahad', 'yellow', '2', 'False']] 

下面是CSV模块的文档:

https://docs.python.org/3.5/library/csv.html#csv.reader

您提到的array你的问题。我不确定这是你真正想要的。 Python数组必须都包含相同的数据类型。给出的例子没有。下面是Python的数组的文档:

https://docs.python.org/2/library/array.html

希望帮助!

相关问题