从CSV文件构建列表的列表 [英] Building list of lists from CSV file

查看:102
本文介绍了从CSV文件构建列表的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Excel文件(我导出为csv),我想解析,但我有麻烦找到最好的方法。 csv是网络中的计算机列表,以及每个计算机的本地管理员组中有哪些帐户。我做了类似tuples的东西,但每个计算机的帐户数目从1到30.我想构建列表的列表,然后通过每个列表找到应该在那里的帐户(管理员等)并删除它们,这样我就可以导出一个只有帐户不应该是本地管理员,但是的列表。 csv文件的格式如下:

I have an Excel file(that I am exporting as a csv) that I want to parse, but I am having trouble with finding the best way to do it. The csv is a list of computers in my network, and what accounts are in the local administrator group for each one. I have done something similar with tuples, but the number of accounts for each computer range from 1 to 30. I want to build a list of lists, then go through each list to find the accounts that should be there(Administrator, etc.) and delete them, so that I can then export a list of only accounts that shouldn't be a local admin, but are. The csv file is formatted as follows:

"computer1"    Administrator    localadmin    useraccount
"computer2"    localadmin       Administrator 
"computer3"    localadmin       Administrator user2account

任何帮助将不胜感激

编辑:这是我正在使用的代码

Here is the code I am working with

import csv 
import sys #used for passing in the argument
file_name = sys.argv[1] #filename is argument 1
with open(file_name, 'rU') as f:  #opens PW file
    reader = csv.reader(f)
    data = list(list(rec) for rec in csv.reader(f, delimiter=',')) #reads csv into a list of lists
    f.close() #close the csv

for i in range(len(data)):
    print data[i][0] #this alone will print all the computer names
    for j in range(len(data[i])) #Trying to run another for loop to print the usernames
        print data[i][j]

问题是第二个for循环。

The issue is with the second for loop. I want to be able to read across each line and for now, just print them.

推荐答案

这应该会让你在右边track:

This should get you on the right track:

import csv 
import sys #used for passing in the argument
file_name = sys.argv[1] #filename is argument 1
with open(file_name, 'rU') as f:  #opens PW file
    reader = csv.reader(f)
    data = list(list(rec) for rec in csv.reader(f, delimiter=',')) #reads csv into a list of lists

    for row in data:
        print row[0] #this alone will print all the computer names
        for username in row: #Trying to run another for loop to print the usernames
            print username

最后两行将打印所有行(包括计算机)。

Last two lines will print all of the row (including the "computer"). Do

for x in range(1, len(row)):
    print row[x]

...以避免打印计算机两次。

... to avoid printing the computer twice.

注意,当使用with结构时,不需要f.close(),因为在退出with块时资源将自动关闭。

Note that f.close() is not required when using the "with" construct because the resource will automatically be closed when the "with" block is exited.

,我只会这样做:

import csv 
import sys #used for passing in the argument
file_name = sys.argv[1] #filename is argument 1
with open(file_name, 'rU') as f:  #opens PW file
    reader = csv.reader(f)
    # Print every value of every row. 
    for row in reader:
        for value in row: 
            print value

这是一种合理的方法来迭代数据,并且应该给你一个坚实的基础,添加任何进一步的逻辑是必需的。

That's a reasonable way to iterate through the data and should give you a firm basis to add whatever further logic is required.

这篇关于从CSV文件构建列表的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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