-3

I have a list that is structured like so:

[{'Id'}, {'IsDeleted'}, {'MasterRecordId'}, {'Name'}, {'Title'}, {'Company'}]

I want to turn it into a string that looks like this:

Id, IsDeleted, MasterRecordId, Name, Title, Company

I've gone through the top answer here:

Removing a list of characters in string

and a few others.

Here's the code I have so far:

    sf = Salesforce(instance_url='insideigt--tony.sandbox.my.salesforce.com/',
                username='igtdataimport@igt.com.tony', 
                password='TXRZ51rRe',
                security_token='2IagJSrI5H3zxeVjgqy1xAN7d',
                domain='test')

def getValidFeilds( sObject ):
    fields = []

    print(sObject)
    schema = getattr( sf, sObject).describe()

    for fieldDict in schema.get('fields', []):
        fieldType = fieldDict.get('type')
        if fieldType not in ['address', 'location'] and not fieldDict.get('compoundFieldName'):
            fields.append({
                fieldDict.get('name')
            })
    #np.array(fields).flatten()
    chars_to_remove = ["'", "{" ,"}" ]
    my_string= ','.join(map(str, fields))
    my_string.translate(str.maketrans({"'":None}))


    
    print(my_string)
    return fields
sObject = 'Lead'
fields = getValidFeilds(sObject)
khelwood
  • 55,782
  • 14
  • 81
  • 108
joebob
  • 39
  • 6

3 Answers3

0

Here's an updated version of your code that should work:

def getValidFeilds( sObject ):
    fields = []

    schema = getattr( sf, sObject).describe()

    for fieldDict in schema.get('fields', []):
        fieldType = fieldDict.get('type')
        if fieldType not in ['address', 'location'] and not fieldDict.get('compoundFieldName'):
            fields.append(fieldDict.get('name'))

    # Use the `join` function to join the list of fields into a single string,
    # separated by commas
    my_string = ', '.join(fields)

    # Remove the characters that you don't want using the `translate` function
    # This will remove single quotes, curly braces, and spaces from the string
    my_string = my_string.translate(str.maketrans('', '', "'{} "))

    print(my_string)
    return fields

sObject = 'Lead'
fields = getValidFeilds(sObject)
0

I think the curly brackets in your example are representing sets, so your list is actually a list of sets.

This should give you a list of just the values - you could make it a list comprehension instead of a loop if you wanted.

list_of_sets = [{'Id'}, {'IsDeleted'}, {'MasterRecordId'}, {'Name'}, {'Title'}, {'Company'}]

new_list = []
for n in list_of_sets:
    for y in n:
        new_list.append(y)

print(new_list)
alexei7
  • 182
  • 1
  • 3
  • 11
0

you could use join with a comprehension that extracts the word from each set:

L = [{'Id'}, {'IsDeleted'}, {'MasterRecordId'}, {'Name'}, 
     {'Title'}, {'Company'}]

S = " ".join( word for word,*_ in L )

print(S)
Id IsDeleted MasterRecordId Name Title Company
Alain T.
  • 40,517
  • 4
  • 31
  • 51