Python从列表转换为字符串后删除字符串中的字符。

huangapple go评论66阅读模式
英文:

Python remove characters in string that was converted from list

问题

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

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 getValidFields(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'))

    my_string = ', '.join(fields)

    print(my_string)
    return fields

sObject = 'Lead'
fields = getValidFields(sObject)

I hope this helps!

英文:

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:

https://stackoverflow.com/questions/10017147/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)

答案1

得分: 0

这是您的代码的更新版本,应该可以工作:

def getValidFields(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'))

    # 使用 `join` 函数将字段列表连接成一个字符串,用逗号分隔
    my_string = ', '.join(fields)

    # 使用 `translate` 函数删除不想要的字符
    # 这将从字符串中删除单引号、花括号和空格
    my_string = my_string.translate(str.maketrans('', '', "'{} "))

    print(my_string)
    return fields

sObject = 'Lead'
fields = getValidFields(sObject)
英文:

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)

答案2

得分: 0

我认为你示例中的花括号表示集合,所以你的列表实际上是一个集合列表。

这将为您提供一个只包含值的列表 - 如果您愿意,您可以将其改为列表理解而不是循环。

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)
英文:

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)

答案3

得分: 0

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

print(S)
Id IsDeleted MasterRecordId Name Title Company

英文:

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

huangapple
  • 本文由 发表于 2023年3月7日 03:17:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654942.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定