Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building list of lists from CSV file

Tags:

python

list

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

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.

like image 763
Michael Dornisch Avatar asked Nov 07 '13 14:11

Michael Dornisch


People also ask

How do I make a list in csv?

The most common method to write data from a list to CSV file is the writerow() method of writer and DictWriter class. Example 1: Creating a CSV file and writing data row-wise into it using writer class.

Does csv reader create a list?

The csv. reader creates a list for each line in the file. Which makes sense in most cases, because most csv files aren't a single column.

How do I append a CSV file to a list?

Once the writer object is created, we can append the list to the csv file using the csv. writerow() method. The csv. writerow() method, when invoked on a writer object, takes a list as its input argument and appends it to the csv file referred by the writer object.


2 Answers

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.

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.

like image 190
ben_frankly Avatar answered Oct 12 '22 18:10

ben_frankly


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

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]
like image 23
mauve Avatar answered Oct 12 '22 17:10

mauve