在Python中删除列表中的特定字符

在Python中删除列表中的特定字符,python,dictionary,Python,Dictionary,我有一个列表,里面有几个单词,它们都用逗号“,”隔开。当我创建这个列表时,我在for循环中,在每个dict之后添加一个逗号来分隔它们,但它也在最后一个dictionary之后添加最后一个逗号。比如: "guests": [{ "age": "18", "birthDate": null, "emailAddress": null,... .... "name": { "prefix": "Mr.", "firstName": "James"

我有一个列表,里面有几个单词,它们都用逗号“,”隔开。当我创建这个列表时,我在for循环中,在每个dict之后添加一个逗号来分隔它们,但它也在最后一个dictionary之后添加最后一个逗号。比如:

"guests": [{
    "age": "18",
    "birthDate": null,
    "emailAddress": null,...
    ....
    "name": {
    "prefix": "Mr.",
    "firstName": "James",
    "middleName": "",
    "lastName": "Jones",
    "suffix": ""
    }
},----------------------------->This comma
]
我认为最后一个逗号在尝试向web服务发出post请求时会产生一些问题。那么,我如何删除列表中的最后一个逗号呢

谢谢

编辑

列表的创建是在for循环中进行的。比如:

participants_body = ''

for guest in guests_info:

        post_body = '{"profile": {"name": {"title": "' + guest["title"] + '","firstName": "' \
                           + guest["first_name"] + '","lastName": "' + guest["last_name"] \
                           + '"},"age": 18},"preferences": {"avatarIdentifier": "15655408","favoriteCharacterIdentifier":' \
                             ' "15655408"},"friendsAndFamily": {"groupClassification": {"name": "TRAVELLING_PARTY"},' \
                             '"accessClassification": {"name": "PLAN_VIEW_SHARED"}}}'

        response = requests.post(url, data=post_body, headers=headers)

        json_response = response.json()

       participants_body = '{"age": "' + str(json_response["profile"]["age"]) + '","birthDate": null,"emailAddress": null,' \
                            '"phone": null,"primary": false,"swid": null,"guid": "' + guid + '","gender": null,"type": null,' \
                            '"participantId": "' + p_id + '","profileLink": "https://env5.nge.api.go.com' + profileLink + '", ' \
                            '"infantSittingWithAdult": false,"avatar": null,"itemsAssigned": ' \
                            '["' + item_id + '"],"address": null,"phoneNumber": null,"dataType": "basic",' \
                            '"isRoomAssigned": true,"isVacationOfferAssigned": true,"ageGroup": "","name": {' \
                            '"prefix": "' + json_response["profile"]["name"]["title"] + '","firstName": "' \
                            + json_response["profile"]["name"]["firstName"] + '","middleName": "","lastName": "' \
                            +

                           json_response["profile"]["name"]["lastName"] + '","suffix": ""}},'------------> HERE IS THE COMA

        post_body_participants += participants_body
所以,这就是我昏迷的原因。我只需要在for循环之后删除它

编辑

我正在创建一条Post消息,我收到了以下错误:

{u'errors': [{u'message': u'org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class com.disney.wdpro.service.booking.webservice.resource.ParticipantWithAssignmentResourceCollection] from JSON String; no single-String constructor/factory method'}]}
我读了几个问题,他们提到这可能是因为json格式出错


此外,我还可以看到日志中的其他消息是如何创建文章正文的,而最后一个逗号不在那里,所以可能就是这样发生的

我不确定为什么要将其创建为字符串。您将更乐于创建dicts作为dicts。代码更具可读性,这将有助于您以后更改代码。除此之外,它还可以消除你正在经历的一些打字错误

post_body = {
    'profile': {
        'name': {
            'title': guest['title'],
            'firstName': guest['first_name'],
            'lastName': guest['last_name'] },
        'age': 18 },
    'preferences': {
        'avatarIdentifier': 15655408,
        'favoriteCharacterIdentifier': 15655408 },
    'friendsAndFamily': {
        'groupClassification': {
            'name': 'TRAVELLING_PARTY' },
        'accessClassification': {
            'name': 'PLAN_VIEW_SHARED' }
    }
}
很容易将该dict转换为JSON字符串:

import json
post_body = json.dumps(post_body)

您可以通过
参与者\u body
响应创建一个列表来完成相同的操作。只需像上面那样创建一个dict,并用
post\u body\u participants.append(participants\u body)
附加它。同样,您可以通过
JSON.dumps(post_body_参与者)

以JSON字符串的形式访问该列表,如果您使用内置的JSON编码器/解码器构建JSON字符串,您将省去大量的痛苦。手工构建它们容易出错。为什么不站在巨人的肩膀上

import requests
import json

participants =[]

for guest in guests_info:
    #Build Python objects and not json strings
    #Convert it all to json later
    post_body = {
        'profile': {
            'name': {
                'title': guest['title'],
                'firstName': guest['first_name'],
                'lastName': guest['last_name'] },
            'age': 18 },
        'preferences': {
            'avatarIdentifier': 15655408,
            'favoriteCharacterIdentifier': 15655408 },
        'friendsAndFamily': {
            'groupClassification': {
                'name': 'TRAVELLING_PARTY' },
            'accessClassification': {
                'name': 'PLAN_VIEW_SHARED' }
        }
    }

    #The requests module has json encoding/decoding built in
    response = requests.post(url, json=post_body, headers=headers)  
    #Or you could use Python's built in json module
    #response = requests.post(url, data=json.dumps(post_body), headers=headers)  

    json_response = response.json() #This decodes the json string in the response to a Python object


    participant = {
        "age": json_response["profile"]["age"],
        "birthDate": None,
        "emailAddress": None,
        "phone": None,
        "primary": False,
        "swid": None,
        "guid": guid,
        "gender": None,
        "type": None,
        "participantId": p_id,
        "profileLink": "https://env5.nge.api.go.com" + profileLink + ,
        "infantSittingWithAdult": False,
        "avatar": None,
        "itemsAssigned": [item_id],
        "address": None,
        "phoneNumber": None,
        "dataType": "basic",
        "isRoomAssigned": True,
        "isVacationOfferAssigned": True,
        "ageGroup": "",
        "name": {
            "prefix": json_response["profile"]["name"]["title"],
            "firstName": json_response["profile"]["name"]["firstName"],
            "middleName": "",
            "lastName": json_response["profile"]["name"]["lastName"],
            "suffix": ""}
        }
    }

    participants.append(participant)

这是实际的字典列表还是字符串?如果您正在谈论构建这样的字符串,请尝试
“,”。加入(dict字符串列表)
。另外,您是否尝试过该模块,特别是
json.dumps
?该模块是如何构建的?为什么你认为逗号在文章中是个问题?你是如何发布数据的?这很奇怪,因为python会自动为你删除这个逗号。请省心,不要手动构建json字符串。构建Python对象,然后使用json.dumps。我将其创建为列表,因为这是web服务所要检查的。它将使您的生活变得更加简单,可以创建为dict或列表,然后将其转换为字符串。@user3799942-我键入了类似的答案,但Greg打败了我,他的答案更好。我建议你接受他的建议。也许我遗漏了什么,但是
null
true
false
都不是有效的Python关键字。我认为应该是
None
True
False
<代码>json。转储文件将相应地转换这些文件。