读取CSV文件并将其插入python中的2d列表中 [英] reading CSV file and inserting it into 2d list in python

查看:88
本文介绍了读取CSV文件并将其插入python中的2d列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将CSV文件的数据(诸如时间,IP地址,端口号之类的网络数据)插入Python的2D列表中.

I want to insert the data of CSV file (network data such as: time, IP address, port number) into 2D list in Python.

这是代码:

import csv
datafile = open('a.csv', 'r')
datareader = csv.reader(datafile, delimiter=';')
data = []
for row in datareader:
    data.append(row)    
print (data[1:4])

结果是:

[['1', '6', '192.168.4.118', '1605', '', '115.85.145.5', '80', '', '60', '0.000000000', '0x0010', 'Jun 15, 2010 18:27:57.490835000', '0.000000000'], 
['2', '6','115.85.145.5', '80', '', '192.168.4.118', '1605', '', '1514', '0.002365000', '0x0010', 'Jun 15, 2010 18:27:57.493200000', '0.002365000'], 
['3', '6', '115.85.145.5', '80', '', '192.168.4.118', '1605', '', '1514', '0.003513000', '0x0018', 'Jun 15, 2010 18:27:57.496713000', '0.005878000']]

但这只是一个维度,我不知道如何创建2D数组并将每个元素插入到数组中.

But it is just one dimension and I don't know how to create 2D array and insert each element into the array.

请建议我为此目的使用什么代码. (我查看了网站上的先前提示,但没有一个对我有用)

Please suggest me what code should I use for this purpose. (I looked the previous hints in the website but none of them worked for me)

推荐答案

您已经有了列表列表,它是一种2D数组,您可以像处理一个数据[1] [1]一样对它进行寻址.

You already have list of lists, which is sort of 2D array and you can address it like one data[1][1], etc.

这篇关于读取CSV文件并将其插入python中的2d列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆