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



我有字符串文字,比如说"测试字符串"。我可以简单地执行r'test string'来预加前缀。

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

这是我的json-embedded字符串(我相信它是使用twitter API抓取的):

{"created_at":"Fri Mar 21 15:27:05 +0000 2014","id":447031650824646656,"id_str":"447031650824646656","text":"u4ecau65e5u304au7948u308au3057u3066u304du305fu304bu3089u306d http://t.co/EVgVEOyF2Q","source":"u003ca href="http://twitter.com/download/iphone" rel="nofollow"u003eTwitter for iPhoneu003c/au003e","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":"u30c8u30e1u6ca2","screen_name":"tome_sawa","location":"u8c6au5dde","url":"http://riceriot.tumblr.com/","description":"SNKuff08u5208u308au4e0au3052u30b5u30f3u30c9uff09/APHuff08u672cu7530u69d8 u5cf6u56fd u4e00u4ebau307cu3063u3061u540cu76dfuff09/u8150/u6d77u5916u30cdu30bf/u6210u4ebau6e08/u81eau7531u3067u3059/ u6210u4ebau63a8u5968 /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":"u4ecau65e5u304au7948u308au3057u3066u304du305fu304bu3089u306d http://t.co/EVgVEOyF2Q","source":"u003ca href="http://twitter.com/download/iphone" rel="nofollow"u003eTwitter for iPhoneu003c/au003e","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":"u30c8u30e1u6ca2","screen_name":"tome_sawa","location":"u8c6au5dde","url":"http://riceriot.tumblr.com/","description":"SNKuff08u5208u308au4e0au3052u30b5u30f3u30c9uff09/APHuff08u672cu7530u69d8 u5cf6u56fd u4e00u4ebau307cu3063u3061u540cu76dfuff09/u8150/u6d77u5916u30cdu30bf/u6210u4ebau6e08/u81eau7531u3067u3059/ u6210u4ebau63a8u5968 /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。

好的,所以您的问题似乎是您的输入字符串不是有效的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'"}

但也许你需要双引号。首先还不清楚你是如何获得这些数据的——也许你可以访问一个没有这些问题的更"原始"的输入版本。但一旦你到了这里,你就不能简单地通过"预处理r"来解决它,因为只有当你有一个文字字符串时,这才有意义。

最新更新