Listing Lists from a CSV File

I have an Excel file (which I export as csv) that I want to parse, but I'm having trouble finding the best way to do this. Csv is a list of computers on my network and which accounts are in the local administrators group for each of them. I did something similar with tuples, but the number of accounts for each computer varies from 1 to 30. I want to create 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 the list of accounts only, which should not be the local administrator, but exist. 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'm using

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 problem is with the second cycle. I want to be able to read each line and for now, just print them.

+4
source share
2 answers

This will lead you to the right path:

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

The last two lines will print the entire line (including "computer"). At

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

... not to print the computer twice.

Please note that f.close () is not required when using the "with" construct, because the resource will be automatically closed when exiting the "c" block.

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

This is a smart way to iterate over data and should give you a solid foundation for adding any extra logic.

+7
source

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]
+1

All Articles