Read from csv file

I have a list of worksets in csv file
and i’m trying to create those worksets in revit with
python script based on @eirannejad script.

please help with the errors i’m new to python and revit API.

the code:

“”“Create worksets from a list and set defautl visibility”""

import csv

from Autodesk.Revit import DB

def set_workset_default_visiblity(d, workset, state):
wdvs = DB.WorksetDefaultVisibilitySettings.GetWorksetDefaultVisibilitySettings(d)
wdvs.SetWorksetVisibility(workset.Id, state)

def create_workset(d, name):
return DB.Workset.Create(d, name)

workset_names = []

Path = ‘D:\Test\worksets.csv’

with open(Path, encoding=‘utf8’, newline=’’) as csvfile:
reader = csv.DictReader(csvfile)

for row in reader:
    """print(row['WorksetName'])"""
    workset_names.append(row['WorksetName'])

print(workset_names)

if doc.IsWorkshared:
t = DB.Transaction(doc, “Create Worksets”)
t.Start()

# make your model changes here
for name in workset_names:
    ws = create_workset(doc, name)
    """if "Hidden" in name:
        set_workset_default_visiblity(doc, ws, False)"""

t.Commit()

I did not try your code but the RPS tells you to change the encoding argument of the open() function

change your with open ([...], encoding='utf-8',[...])

at least that is what the Exception Thrown mentions on the first line after the >>> in the RPS windows. Advice here, these exceptions are usually fairly explicit if you take the time to read it. in your case _ even though it is not always the case, it is quite explicit:
open() has a bad keyword argument for the encoding argument.

1 Like

The open that you are using is the bultin lowlevel open function in python. The one that has the encoding= parameter, is from the codecs module that is designed to wrap the builtin open to allow for automatic handling of various encodings:

import codecs

with codecs.open(file_path, encoding='utf-8') as csv_file_handle:
    # ....
2 Likes

File stands for the full file path with the file name?

Yes. just like the builtin open() function