python保存数据到本地文件的方法

作者:rosefun96 时间:2023-10-01 20:59:53 

1、保存列表为.txt文件


#1/list写入txt

ipTable = ['158.59.194.213', '18.9.14.13', '58.59.14.21']
fileObject = open('sampleList.txt', 'w')
for ip in ipTable:
fileObject.write(ip)
fileObject.write('\n')
fileObject.close()

2、字典保存


#2/dict写入json
import json

dictObj = {
'andy':{
 'age': 23,
 'city': 'shanghai',
 'skill': 'python'
},
'william': {
 'age': 33,
 'city': 'hangzhou',
 'skill': 'js'
}
}

jsObj = json.dumps(dictObj)

fileObject = open('jsonFile.json', 'w')
fileObject.write(jsObj)
fileObject.close()

来源:https://blog.csdn.net/rosefun96/article/details/78877452

标签:python,数据,文件
0
投稿

猜你喜欢

  • python定时关机小脚本

    2022-09-24 23:38:21
  • Python不使用int()函数把字符串转换为数字的方法

    2022-04-22 02:32:33
  • 详解python之配置日志的几种方式

    2022-11-02 14:32:30
  • 如何使用django的MTV开发模式返回一个网页

    2023-07-04 21:47:06
  • python使用递归的方式建立二叉树

    2021-07-07 23:47:18
  • 启动targetcli时遇到错误解决办法

    2023-03-20 05:59:43
  • Python使用os模块实现更高效地读写文件

    2021-08-11 00:41:39
  • 解决linux下使用python打开terminal时报错的问题

    2022-10-06 10:46:25
  • asp如何读取服务器上的驱动器?

    2009-11-18 20:50:00
  • Python爬虫信息输入及页面的切换方法

    2023-08-02 17:33:33
  • 火遍全网的Python二次元特效轻松掌握

    2022-01-29 22:01:32
  • php获取referer防非法访问

    2023-08-20 11:30:58
  • 关于adfuller函数返回值的参数说明与记录

    2023-03-07 13:33:14
  • 读写xml文件的2个小函数

    2007-08-23 12:59:00
  • Python全栈之学习HTML

    2023-12-05 14:56:55
  • Python数据分析之Matplotlib数据可视化

    2022-03-05 06:37:48
  • python集合的创建、添加及删除操作示例

    2022-07-09 13:29:38
  • opencv+python实现鼠标点击图像,输出该点的RGB和HSV值

    2023-05-16 13:37:20
  • Python把图片转化为pdf代码实例

    2021-04-05 19:06:36
  • Python3中的真除和Floor除法用法分析

    2023-10-11 09:01:45
  • asp之家 网络编程 m.aspxhome.com