Read and extract info from Shared Parameter file

Hi everyone,

I created a function in Python to open a shared parameter file, read it, search for strings that represent Revit parameter names (user input) and then return their corresponding GUIDS and names through a dictionary.
The syntax used for opening the shared parameter file in Python 3:

with open(originalSharedParameterFile, "r", encoding="utf-16") as fileToRead:

While using Dynamo I couldn’t reproduce the same behavior. According to IronPython documentation 2. Built-in Functions — IronPython 2.7.2b1 documentation there is no third argument encoding so I couldn’t use the same strategy. Somehow the find() method was returning something completely different.

I did find a workaround to make it work, but I don’t want to use it because everything has to be done manually:

  1. Opening the original shared parameter file;
  2. Copy its content;
  3. Create a new .txt file;
  4. Save it;
  5. Point the function to the new file.

I also know that there are Dynamo packages outputting results that I am looking for, but I would like to understand what can be done to make my code work. I am assuming that this is only a problem with the shared parameter encoding.

def find_parameterGuid(parameterToCheck, sharedParameterFile):
    """Check the Guid corresponding to the parameter name. """  \
        """If one Guid is not found, it returns a string """ \
        """describing it."""
   
    messageToUser = []
    parameterNames = []
    parameterGuids = []

    with open(sharedParameterFile, "r") as fileToRead:
        lines = fileToRead.readlines()
        for line in lines:
            # Find the position of the last TAB after the parameter name
            # It should start at index 43 since it is the position of the 
            # First TAB IMMEDIATELY before the parameter name
            startingIndexOfGuid = 6
            startingIndexOfTab = 43
            for p in parameterToCheck:
                stopingIndexOfTab = line.find("\t", startingIndexOfTab)
                startingIndexOfParameter = line.find(p, startingIndexOfTab, stopingIndexOfTab)
                # Here we evaluate the result as different of -1. The -1 represents
                # That no index was found, thus no parameter found. By consequence if 
                # Is NOT -1, than the parameter was found
                if startingIndexOfParameter != -1:
                    guid = line[startingIndexOfGuid: startingIndexOfTab -1]
                    parameterName = line[startingIndexOfTab: stopingIndexOfTab]
                    # The a similar principle is here applied. If there is NO 
                    # White space in the checked parameter then return a similar result
                    # Otherwise return parameters that have a white space in their strings
                    if p.find(" ") == -1 and parameterName.find(" ") == -1:
                        parameterNames.append(parameterName)
                        parameterGuids.append(guid)
                    elif p.find(" ") != -1 and parameterName.find(" ") != -1:
                        parameterNames.append(parameterName)
                        parameterGuids.append(guid)

    # Creating the dictionary that is the base for all the operations
    # First create a iterator combining the two lists
    zip_iterator = zip(parameterNames, parameterGuids)
    # Then use dict to convert the iterator to a dictionary
    sharedPar_dictionary = dict(zip_iterator)
    
    # Also set a message to the user if the parameters they have chosen
    # Are NOT in shared parameter files. This can be obtained 
    # If I check the result of this IF statement against
    # The user input
    for p in parameterToCheck:
        if p not in parameterNames:
            messageToUser.append(p + " NOT in the selected shared parameters"\
                "text file. Please check this before continuing."\
                " Script not ran.")
    # What this definition returns
    if len(messageToUser) == 0:
        return sharedPar_dictionary
    else:
        return messageToUser

Thank you in advance

Hi,
an example using codecs and csv module

import sys
import clr
import System
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
pf_path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86)
sys.path.append(pf_path + '\\IronPython 2.7\\Lib')
import codecs
import csv

codec_reader = "utf-16"
with codecs.open(IN[0], "r", encoding = codec_reader) as fileToRead:
	data_reader = csv.reader(fileToRead.readlines(), delimiter='\t')

OUT = data_reader

@c.poupin thank you for your help. I just had to adjust a bit your code as I don’t want to separate the lines per TAB. I literally want to have the same structure as on the txt. file:

So:

    with codecs.open(sharedParameterFile, "r", encoding= codec_reader) as fileToRead:
        lines = csv.reader(fileToRead.readlines(), delimiter= '\n')
        # https://stackoverflow.com/questions/29244286/how-to-flatten-a-2d-list-to-1d-without-using-numpy
        lines_flattened = [j for sub in lines for j in sub]
        for line in lines_flattened:
 # And then the rest of the code

Now I can read the file as I want :slight_smile:

1 Like