Python 从 CSV 文件构建列表列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19838380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:45:45  来源:igfitidea点击:

Building list of lists from CSV file

pythonlistcsv

提问by Michael Dornisch

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:

我有一个想要解析的 Excel 文件(我将其导出为 csv),但我无法找到最佳方法。csv 是我网络中的计算机列表,以及每个计算机在本地管理员组中的帐户。我对元组做了类似的事情,但是每台计算机的帐户数从 1 到 30 不等。我想建立一个列表列表,然后遍历每个列表以找到应该在那里的帐户(管理员等)并删除它们,这样我就可以导出一个不应该是本地管理员的帐户列表,而是。csv 文件的格式如下:

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

Any help would be appreciated

任何帮助,将不胜感激

EDIT: 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]

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

问题在于第二个 for 循环。我希望能够阅读每一行,现在,只需打印它们。

采纳答案by ben_frankly

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.

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

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.

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

Personally, I would just do:

就个人而言,我只会这样做:

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.

这是迭代数据的合理方法,应该为您添加所需的任何进一步逻辑提供坚实的基础。

回答by mauve

This is how I opened a .csv file and imported columns of data as numpy arrays - naturally, you don't need numpy arrays, but...

这就是我打开 .csv 文件并将数据列导入为 numpy 数组的方式 - 自然,您不需要 numpy 数组,但是......

data = {}

app = QApplication( sys.argv )
fname = unicode ( QFileDialog.getOpenFileName() )
app.quit()
filename = fname.strip('.csv') + ' for release.csv'

#open the file and skip the first two rows of data
imported_array = np.loadtxt(fname, delimiter=',', skiprows = 2)

data = {'time_s':imported_array[:,0]}
data['Speed_RPM'] = imported_array[:,1]