<i id='UMTuk'><tr id='UMTuk'><dt id='UMTuk'><q id='UMTuk'><span id='UMTuk'><b id='UMTuk'><form id='UMTuk'><ins id='UMTuk'></ins><ul id='UMTuk'></ul><sub id='UMTuk'></sub></form><legend id='UMTuk'></legend><bdo id='UMTuk'><pre id='UMTuk'><center id='UMTuk'></center></pre></bdo></b><th id='UMTuk'></th></span></q></dt></tr></i><div id='UMTuk'><tfoot id='UMTuk'></tfoot><dl id='UMTuk'><fieldset id='UMTuk'></fieldset></dl></div>
<legend id='UMTuk'><style id='UMTuk'><dir id='UMTuk'><q id='UMTuk'></q></dir></style></legend>
    <bdo id='UMTuk'></bdo><ul id='UMTuk'></ul>
  • <small id='UMTuk'></small><noframes id='UMTuk'>

    1. <tfoot id='UMTuk'></tfoot>

        JSON格式在我附加文件时添加字符,但不是输出中的字符串

        JSON formatting adding characters when I append file, but not to string in output(JSON格式在我附加文件时添加字符,但不是输出中的字符串)

          <i id='MPZn6'><tr id='MPZn6'><dt id='MPZn6'><q id='MPZn6'><span id='MPZn6'><b id='MPZn6'><form id='MPZn6'><ins id='MPZn6'></ins><ul id='MPZn6'></ul><sub id='MPZn6'></sub></form><legend id='MPZn6'></legend><bdo id='MPZn6'><pre id='MPZn6'><center id='MPZn6'></center></pre></bdo></b><th id='MPZn6'></th></span></q></dt></tr></i><div id='MPZn6'><tfoot id='MPZn6'></tfoot><dl id='MPZn6'><fieldset id='MPZn6'></fieldset></dl></div>
            <bdo id='MPZn6'></bdo><ul id='MPZn6'></ul>

            <small id='MPZn6'></small><noframes id='MPZn6'>

              <legend id='MPZn6'><style id='MPZn6'><dir id='MPZn6'><q id='MPZn6'></q></dir></style></legend>

                <tbody id='MPZn6'></tbody>

                • <tfoot id='MPZn6'></tfoot>
                • 本文介绍了JSON格式在我附加文件时添加字符,但不是输出中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用以下函数从 flickr API 获取 json.它返回的字符串是格式正确的 JSON 块:

                  I am using the following function to get json from the flickr API. The string it returns is a properly formatted chunk of JSON:

                  def get_photo_data(photo_id):
                      para = {}
                      para["photo_id"] = photo_id
                      para["method"] = "flickr.photos.getInfo"
                      para["format"] = "json"
                      para["api_key"] = FLICKR_KEY
                      request_data = params_unique_combination("https://api.flickr.com/services/rest/", para)
                  
                      if request_data in CACHE_DICTION:
                          return CACHE_DICTION[request_data]
                      else:
                          response = requests.get("https://api.flickr.com/services/rest/", para)
                          CACHE_DICTION[request_data] = response.text[14:-1]
                          cache_file = open(CACHE_FNAME, 'w')
                          cache_file.write(json.dumps(CACHE_DICTION))
                          cache_file.close()
                          return response.text[14:-1]
                  

                  我遇到的问题是,当我将 json 写入缓存文件时,它会不断添加反斜杠,例如以下示例:

                  The issue I am having is that when I go to write the json to my cache file it keeps adding in backslashes, like this example:

                  "https://api.flickr.com/services/rest/format-json_method-flickr.photos.getInfo_photo_id-34869402493": "{"photo":{"id":"34869402493","secret":"56fcf0342c","server":"4057","farm":5,"dateuploaded":"1499030213","isfavorite":0,"license":"0","safety_level":"0","rotation":0,"originalsecret":"c4d1d316ed","originalformat":"jpg","owner":{"nsid":"150544082@N05","username":"ankitrana_","realname":"Ankit Rana","location":"美国辛辛那提","iconserver":"4236","iconfarm":5,"path_alias":"ankitrana_"},"title":{"_content":"7"},"description":{"_content":""},"visibility":{"ispublic":1,"isfriend":0,"isfamily":0},"dates":{"posted":"1499030213","taken":"2017-06-19 13:43:38","takengranularity":"0","takenunknown":"0","lastupdate":"1499041020"},"views":"41","editability":{"cancomment":0,"canaddmeta":0},"publiceditability":{"cancomment":1,"canaddmeta":0},"用法":{"candownload":1,"canblog":0,"canprint":0,"canshare":1},"comments":{"_content":"0"},"notes":{"note":[]},"people":{"haspeople":0},"tags":{"tag":[{"id":"150538742-34869402493-5630","作者":"150544082@N05","作者名":"ankitrana_","raw":"cincinnati","_content":"cincinnati","machine_tag":0},{"id":"150538742-34869402493-226","作者":"150544082@N05","作者名":"ankitrana_","raw":"ohio","_content":"ohio","machine_tag":false},...等等等等}

                  如何在没有这些额外的 字符的情况下将 JSON 存储到现有文件中,就像我打印字符串时所表示的那样?

                  How can I store the JSON to the existing file without these additional characters, as it is represented when I print the string?

                  推荐答案

                  使用 your_string.decode('string_escape')" 转义为 "

                  use your_string.decode('string_escape') to unescape " to "

                  更新:

                  你的字符串转义是因为 json.dumps(),它将对象转换为字符串,然后你可以使用 json.loads() 读取它,结果未转义.

                  your string escaped because json.dumps(), it convert object to string and later you can read it using json.loads(), the result are unescaped.

                  你可以使用 str() 保存它而不使用斜线

                  you can save it without slash using str()

                  cache_file.write(str(CACHE_DICTION))
                  # {'myparam' :'"162000","photo":...'
                  

                  但是它用单引号保存到文件的问题,它不是有效的 json 并且与 json.loads()

                  but the problem it save to file with single quotes, it not valid json and not compatible with json.loads()

                  我的建议保持您的代码如上,除非您想将其存储到文件 CACHE_FNAME.json

                  my suggestion keep your code as above, except you want to store it to file CACHE_FNAME.json

                  cache_file = open(CACHE_FNAME, 'w')
                  cache_file.write(response.text)
                  cache_file.close()
                  # {"photos":{"page":1,"pages":6478,..}
                  

                  这篇关于JSON格式在我附加文件时添加字符,但不是输出中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                  Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                  python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                  Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                  How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                  Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)

                      <tbody id='xWs7N'></tbody>
                    • <bdo id='xWs7N'></bdo><ul id='xWs7N'></ul>

                      • <tfoot id='xWs7N'></tfoot>

                        <i id='xWs7N'><tr id='xWs7N'><dt id='xWs7N'><q id='xWs7N'><span id='xWs7N'><b id='xWs7N'><form id='xWs7N'><ins id='xWs7N'></ins><ul id='xWs7N'></ul><sub id='xWs7N'></sub></form><legend id='xWs7N'></legend><bdo id='xWs7N'><pre id='xWs7N'><center id='xWs7N'></center></pre></bdo></b><th id='xWs7N'></th></span></q></dt></tr></i><div id='xWs7N'><tfoot id='xWs7N'></tfoot><dl id='xWs7N'><fieldset id='xWs7N'></fieldset></dl></div>
                        <legend id='xWs7N'><style id='xWs7N'><dir id='xWs7N'><q id='xWs7N'></q></dir></style></legend>

                            <small id='xWs7N'></small><noframes id='xWs7N'>