当字符串存储为变量时,如何在python字符串前加前缀?

时间:2014-10-21 01:18:56

标签: python string

我有字符串文字,让我们说'测试字符串'。我可以简单地r'test string'添加前缀。

但是,现在我有s = 'test string'。在这种情况下,如何将PREFIX r(不是字符串' r')添加到此字符串中?

这是我的json嵌入式字符串(使用我认为的推特API抓取它):

{"created_at":"Fri Mar 21 15:27:05 +0000 2014","id":447031650824646656,"id_str":"447031650824646656","text":"\u4eca\u65e5\u304a\u7948\u308a\u3057\u3066\u304d\u305f\u304b\u3089\u306d http:\/\/t.co\/EVgVEOyF2Q","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1615583810,"id_str":"1615583810","name":"\u30c8\u30e1\u6ca2","screen_name":"tome_sawa","location":"\u8c6a\u5dde","url":"http:\/\/riceriot.tumblr.com\/","description":"SNK\uff08\u5208\u308a\u4e0a\u3052\u30b5\u30f3\u30c9\uff09\/APH\uff08\u672c\u7530\u69d8 \u5cf6\u56fd \u4e00\u4eba\u307c\u3063\u3061\u540c\u76df\uff09\/\u8150\/\u6d77\u5916\u30cd\u30bf\/\u6210\u4eba\u6e08\/\u81ea\u7531\u3067\u3059\/ \u6210\u4eba\u63a8\u5968 \/pixiv id=2452371\/ http:\/\/ask.fm\/tomesawa","protected":false,"followers_count":323,"friends_count":93,"listed_count":15,"created_at":"Tue Jul 23 16:04:57 +0000 2013","favourites_count":835,"utc_offset":32400,"time_zone":"Irkutsk","geo_enabled":false,"verified":false,"statuses_count":9199,"lang":"ja","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/378800000120467721\/5ba3a7a1a51c27d5846a21497ed626e3.png","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/378800000120467721\/5ba3a7a1a51c27d5846a21497ed626e3.png","profile_background_tile":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/435018392261513216\/rDsSbqDS_normal.png","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/435018392261513216\/rDsSbqDS_normal.png","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1615583810\/1375715189","profile_link_color":"0084B4","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[],"media":[{"id":447031650489081856,"id_str":"447031650489081856","indices":[13,35],"media_url":"http:\/\/pbs.twimg.com\/media\/BjQs6urCIAAl3Gk.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/BjQs6urCIAAl3Gk.jpg","url":"http:\/\/t.co\/EVgVEOyF2Q","display_url":"pic.twitter.com\/EVgVEOyF2Q","expanded_url":"http:\/\/twitter.com\/tome_sawa\/status\/447031650824646656\/photo\/1","type":"photo","sizes":{"medium":{"w":600,"h":600,"resize":"fit"},"large":{"w":1024,"h":1024,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"small":{"w":340,"h":340,"resize":"fit"}}}]},"favorited":false,"retweeted":false,"possibly_sensitive":true,"filter_level":"medium","lang":"ja"}

我尝试了以下代码:

s = '{"created_at":"Fri Mar 21 15:27:05 +0000 2014","id":447031650824646656,"id_str":"447031650824646656","text":"\u4eca\u65e5\u304a\u7948\u308a\u3057\u3066\u304d\u305f\u304b\u3089\u306d http:\/\/t.co\/EVgVEOyF2Q","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1615583810,"id_str":"1615583810","name":"\u30c8\u30e1\u6ca2","screen_name":"tome_sawa","location":"\u8c6a\u5dde","url":"http:\/\/riceriot.tumblr.com\/","description":"SNK\uff08\u5208\u308a\u4e0a\u3052\u30b5\u30f3\u30c9\uff09\/APH\uff08\u672c\u7530\u69d8 \u5cf6\u56fd \u4e00\u4eba\u307c\u3063\u3061\u540c\u76df\uff09\/\u8150\/\u6d77\u5916\u30cd\u30bf\/\u6210\u4eba\u6e08\/\u81ea\u7531\u3067\u3059\/ \u6210\u4eba\u63a8\u5968 \/pixiv id=2452371\/ http:\/\/ask.fm\/tomesawa","protected":false,"followers_count":323,"friends_count":93,"listed_count":15,"created_at":"Tue Jul 23 16:04:57 +0000 2013","favourites_count":835,"utc_offset":32400,"time_zone":"Irkutsk","geo_enabled":false,"verified":false,"statuses_count":9199,"lang":"ja","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/378800000120467721\/5ba3a7a1a51c27d5846a21497ed626e3.png","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/378800000120467721\/5ba3a7a1a51c27d5846a21497ed626e3.png","profile_background_tile":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/435018392261513216\/rDsSbqDS_normal.png","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/435018392261513216\/rDsSbqDS_normal.png","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1615583810\/1375715189","profile_link_color":"0084B4","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[],"media":[{"id":447031650489081856,"id_str":"447031650489081856","indices":[13,35],"media_url":"http:\/\/pbs.twimg.com\/media\/BjQs6urCIAAl3Gk.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/BjQs6urCIAAl3Gk.jpg","url":"http:\/\/t.co\/EVgVEOyF2Q","display_url":"pic.twitter.com\/EVgVEOyF2Q","expanded_url":"http:\/\/twitter.com\/tome_sawa\/status\/447031650824646656\/photo\/1","type":"photo","sizes":{"medium":{"w":600,"h":600,"resize":"fit"},"large":{"w":1024,"h":1024,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"small":{"w":340,"h":340,"resize":"fit"}}}]},"favorited":false,"retweeted":false,"possibly_sensitive":true,"filter_level":"medium","lang":"ja"}'
json.loads(s);

它给了我这个错误:

----> 1 json.loads(s)
.................
ValueError: Expecting , delimiter: line 1 column 234 (char 233)

我的python版本是2.7.7。

1 个答案:

答案 0 :(得分:2)

好的,所以你的问题似乎是你的输入字符串是无效的JSON。以下是我从您添加到问题中的输入中删除的摘录:

{"source":"\\u003ca href="http:\\/\\/twitter.com\\/download\\/iphone"
  rel="nofollow"\\u003eTwitter for iPhone\\u003c\\/a\\u003e"}

如果我解码unicode字符,它看起来像这样:

{"source":"<a href="http:\\/\\/twitter.com\\/download\\/iphone""}

我看到的主要问题是引号:你在另一个引用字符串中有一个带引号的字符串,它们都使用相同的引号字符。这根本行不通。

如果你这样写,它会起作用:

{"source":"<a href=\'http:\\/\\/twitter.com\\/download\\/iphone\'"}

但也许你需要双引号。你不清楚如何首先获得这些数据 - 也许你可以访问更多&#34; raw&#34;输入的版本没有这些问题。但是,一旦你来到这里,你就可以通过简单地预先r&#34;来解决这个问题,因为这样的事情只有在你有文字时才有意义字符串。