Reputation: 411
I want to read data from a CSV file but my output says field values in the rows are "None". It doesn't recognize the values in my CSV file.
Any ideas ?
Here's the code I am using :
import arcpy
import csv
arcpy.env.workspace = "D:"
csvfile = "D:/file.csv"
domains = []
rows = []
with open(csvfile, 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
try:
domains.append(row.get('domain'))
rows.append(row)
except KeyError:
rows = [row]
domains = list(set(domains))
print("Identified %s unique domains" % len(domains))
print("Identified %s codes" % len(rows))
print("Creating codes:")
for row in rows:
print("%s\t%s\t%s" % (row.get('domain'), row.get('code'), row.get('code_description')))
Here's a sample of my csv file, very basic and tab delimitated.
And here's the result i am getting :
Upvotes: 0
Views: 198
Reputation: 5286
Aside from the indentaiton issues, try the following modification:
import arcpy
import csv
arcpy.env.workspace = "D:"
csvfile = "D:/file.csv"
domains = set()
rows = []
with open(csvfile, 'r') as csvfile:
reader = csv.DictReader(csvfile, delimiter=';')
for row in reader:
try:
domains.add(row['domain'])
rows.append(row)
except KeyError:
print("Couldn't parse", row)
print("Identified %s unique domains" % len(domains))
print("Identified %s codes" % len(rows))
print("Creating codes:")
for row in rows:
print("%s\t%s\t%s" % (row.get('domain'), row.get('code'), row.get('code_description')))
Upvotes: 1