python好用的函数、工具等

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jxust_tj/article/details/54377516

将json数据保存为好看的格式(如果直接存的话在vim中只显示一行,那是极其难看啊,中文数据前面貌似不能加u)

#page.json 是保存json数据的文件名,city_dict 是dict类型变量, indent表示缩进
with open('page.json', 'w') as f:
    json.dump(city_dict, f, ensure_ascii=False, indent=2)
# 这里用于普通情况,data表示json字符串。ensure_ascii是控制中文乱码的,如果还是乱码建议看一下数据库(在网页上显示还应看一下网站返回头部的content_type)
        res = json.dumps(json.loads(data), sort_keys=True, ensure_ascii=False, indent=4)


在线解析json的工具 (看起来特别爽啊):

http://www.jsoneditoronline.org/


调用其他文件中的类时:

os.path.realpath(__file__)  # 当前文件的绝对路径
sys.path.append('文件路径')  # 添加路径

输出不缓存

sys.stdout.flush()

在windows下用python读取txt文件内容,注意txt的编码格式设置为utf-8的

在python 2.x 下写 python 3.x 的代码,使用 __future__ 模块,例:

扫描二维码关注公众号,回复: 4345845 查看本文章

from __future__ import unicode_literals # 导入py3的编码新特性




猜你喜欢

转载自blog.csdn.net/jxust_tj/article/details/54377516