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

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

Python remove characters in string that was converted from list

问题

I have a list that is structured like so:

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

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

  1. Id, IsDeleted, MasterRecordId, Name, Title, Company

Here's the code I have so far:

  1. sf = Salesforce(instance_url='insideigt--tony.sandbox.my.salesforce.com/',
  2. username='igtdataimport@igt.com.tony',
  3. password='TXRZ51rRe',
  4. security_token='2IagJSrI5H3zxeVjgqy1xAN7d',
  5. domain='test')
  6. def getValidFields(sObject):
  7. fields = []
  8. print(sObject)
  9. schema = getattr(sf, sObject).describe()
  10. for fieldDict in schema.get('fields', []):
  11. fieldType = fieldDict.get('type')
  12. if fieldType not in ['address', 'location'] and not fieldDict.get('compoundFieldName'):
  13. fields.append(fieldDict.get('name'))
  14. my_string = ', '.join(fields)
  15. print(my_string)
  16. return fields
  17. sObject = 'Lead'
  18. fields = getValidFields(sObject)

I hope this helps!

英文:

I have a list that is structured like so:

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

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

  1. 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:

  1. sf = Salesforce(instance_url='insideigt--tony.sandbox.my.salesforce.com/',
  2. username='igtdataimport@igt.com.tony',
  3. password='TXRZ51rRe',
  4. security_token='2IagJSrI5H3zxeVjgqy1xAN7d',
  5. domain='test')
  6. def getValidFeilds( sObject ):
  7. fields = []
  8. print(sObject)
  9. schema = getattr( sf, sObject).describe()
  10. for fieldDict in schema.get('fields', []):
  11. fieldType = fieldDict.get('type')
  12. if fieldType not in ['address', 'location'] and not fieldDict.get('compoundFieldName'):
  13. fields.append({
  14. fieldDict.get('name')
  15. })
  16. #np.array(fields).flatten()
  17. chars_to_remove = ["'", "{" ,"}" ]
  18. my_string= ','.join(map(str, fields))
  19. my_string.translate(str.maketrans({"'":None}))
  20. print(my_string)
  21. return fields
  22. sObject = 'Lead'
  23. fields = getValidFeilds(sObject)

答案1

得分: 0

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

  1. def getValidFields(sObject):
  2. fields = []
  3. schema = getattr(sf, sObject).describe()
  4. for fieldDict in schema.get('fields', []):
  5. fieldType = fieldDict.get('type')
  6. if fieldType not in ['address', 'location'] and not fieldDict.get('compoundFieldName'):
  7. fields.append(fieldDict.get('name'))
  8. # 使用 `join` 函数将字段列表连接成一个字符串,用逗号分隔
  9. my_string = ', '.join(fields)
  10. # 使用 `translate` 函数删除不想要的字符
  11. # 这将从字符串中删除单引号、花括号和空格
  12. my_string = my_string.translate(str.maketrans('', '', "'{} "))
  13. print(my_string)
  14. return fields
  15. sObject = 'Lead'
  16. fields = getValidFields(sObject)
英文:

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

  1. def getValidFeilds( sObject ):
  2. fields = []
  3. schema = getattr( sf, sObject).describe()
  4. for fieldDict in schema.get('fields', []):
  5. fieldType = fieldDict.get('type')
  6. if fieldType not in ['address', 'location'] and not fieldDict.get('compoundFieldName'):
  7. fields.append(fieldDict.get('name'))
  8. # Use the `join` function to join the list of fields into a single string,
  9. # separated by commas
  10. my_string = ', '.join(fields)
  11. # Remove the characters that you don't want using the `translate` function
  12. # This will remove single quotes, curly braces, and spaces from the string
  13. my_string = my_string.translate(str.maketrans('', '', "'{} "))
  14. print(my_string)
  15. return fields
  16. sObject = 'Lead'
  17. fields = getValidFeilds(sObject)

答案2

得分: 0

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

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

  1. list_of_sets = [{'Id'}, {'IsDeleted'}, {'MasterRecordId'}, {'Name'}, {'Title'}, {'Company'}]
  2. new_list = []
  3. for n in list_of_sets:
  4. for y in n:
  5. new_list.append(y)
  6. 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.

  1. list_of_sets = [{'Id'}, {'IsDeleted'}, {'MasterRecordId'}, {'Name'}, {'Title'}, {'Company'}]
  2. new_list = []
  3. for n in list_of_sets:
  4. for y in n:
  5. new_list.append(y)
  6. 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:

  1. L = [{'Id'}, {'IsDeleted'}, {'MasterRecordId'}, {'Name'},
  2. {'Title'}, {'Company'}]
  3. S = " ".join( word for word,*_ in L )
  4. print(S)
  5. 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:

确定