0

我正在尝试创建一个函数,如果来自 JSON 文件的推文少于 50 个字符,则返回 true,如果大于或等于 50 个字符,则返回 false。

编辑:我的推文存储在 JSON 文件“1_tweets”中。此 JSON 文件中有多个推文。如何将公式应用于此文件?

我会使用什么代码?最容易使用 if 语句?

我要完成的功能如下

def short_t(tweet)

JSON 文件示例

   {
    "contributors": null,
    "coordinates": null,
    "created_at": "Mon May 18 04:28:32 +0000 2020",
    "entities": {
        "hashtags": [],
        "symbols": [],
        "urls": [
            {
                "display_url": "twitter.com/i/web/status/1\u2026",
                "expanded_url": "https://twitter.com/i/web/status/1262238618644611072",
                "indices": [
                    105,
                    128
                ],
                "url": "..."
            }
        ],
        "user_mentions": [
            {
                "id": 819234644352704513,
                "id_str": "819234644352704513",
                "indices": [
                    0,
                    11
                ],
                "name": "John Kish",
                "screen_name": "JohnKish20"
            },
            {
                "id": 203818445,
                "id_str": "203818445",
                "indices": [
                    12,
                    24
                ],
                "name": "Felicia",
                "screen_name": "felicia3354"
            },
            {
                "id": 533306439,
                "id_str": "533306439",
                "indices": [
                    25,
                    33
                ],
                "name": "Jason Richard",
                "screen_name": "pas_bon"
            },
            {
                "id": 38284810,
                "id_str": "38284810",
                "indices": [
                    34,
                    45
                ],
                "name": "toomuch***",
                "screen_name": "hateisevil"
            },
            {
                "id": 3247044122,
                "id_str": "3247044122",
                "indices": [
                    46,
                    61
                ],
                "name": "Noble Prize Winning Hamberder",
                "screen_name": "RedBull_n_Math"
            },
            {
                "id": 208155240,
                "id_str": "208155240",
                "indices": [
                    62,
                    77
                ],
                "name": "Maria Bartiromo",
                "screen_name": "MariaBartiromo"
            },
            {
                "id": 25073877,
                "id_str": "25073877",
                "indices": [
                    78,
                    94
                ],
                "name": "Donald J. Trump",
                "screen_name": "realDonaldTrump"
            },
            {
                "id": 1367531,
                "id_str": "1367531",
                "indices": [
                    95,
                    103
                ],
                "name": "Fox News",
                "screen_name": "FoxNews"
            }
        ]
    },
    "favorite_count": 0,
    "favorited": false,
    "geo": null,
    "id": 1262238618644611072,
    "id_str": "1262238618644611072",
    "in_reply_to_screen_name": "JohnKish20",
    "in_reply_to_status_id": 1262235869114744833,
    "in_reply_to_status_id_str": "1262235869114744833",
    "in_reply_to_user_id": 819234644352704513,
    "in_reply_to_user_id_str": "819234644352704513",
    "is_quote_status": false,
    "lang": "en",
    "metadata": {
        "iso_language_code": "en",
        "result_type": "recent"
    },
    "place": null,
    "retweet_count": 0,
    "retweeted": false,
    "source": "<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone</a>",
    "text": "@JohnKish20 @felicia3354 @pas_bon @hateisevil @RedBull_n_Math @MariaBartiromo @realDonaldTrump @FoxNews\u2026",
    "truncated": true,
    "user": {
        "contributors_enabled": false,
        "created_at": "Sun May 15 13:14:39 +0000 2011",
        "default_profile": true,
        "default_profile_image": false,
        "description": "Love grows, hate destroys. If we can understand, we will love. I follow back. Sorry,but I get too many direct messages to read #MAGA #Trump2020\ud83c\uddfa\ud83c\uddf8\ud83c\uddfa\ud83c\uddf8\ud83c\uddfa\ud83c\uddf8\ud83c\uddfa\ud83c\uddf8",
        "entities": {
            "description": {
                "urls": []
            }
        },
        "favourites_count": 101243,
        "follow_request_sent": false,
        "followers_count": 12486,
        "following": false,
        "friends_count": 13655,
        "geo_enabled": false,
        "has_extended_profile": false,
        "id": 299071087,
        "id_str": "299071087",
        "is_translation_enabled": false,
        "is_translator": false,
        "lang": null,
        "listed_count": 104,
        "location": "New Orleans",
        "name": "Zugly747",
        "notifications": false,
        "profile_background_color": "C0DEED",
        "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png",
        "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png",
        "profile_background_tile": false,
        "profile_banner_url": "https://pbs.twimg.com/profile_banners/299071087/1526558163",
        "profile_image_url": "http://pbs.twimg.com/profile_images/1381022457/image_normal.jpg",
        "profile_image_url_https": "https://pbs.twimg.com/profile_images/1381022457/image_normal.jpg",
        "profile_link_color": "1DA1F2",
        "profile_sidebar_border_color": "C0DEED",
        "profile_sidebar_fill_color": "DDEEF6",
        "profile_text_color": "333333",
        "profile_use_background_image": true,
        "protected": false,
        "screen_name": "zugly747",
        "statuses_count": 137146,
        "time_zone": null,
        "translator_type": "none",
4

1 回答 1

0

只需使用循环遍历 JSON 中返回的每个状态对象。然后检查全文属性的长度是否大于50。

tweet = #Declare tweet

def short_t(tweet):
    if len(tweet) < 50:
        return True
    else:
        return False

for tweet in tweet:
    short_t(tweet.full_text)
于 2020-05-27T06:50:49.297 回答