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

yipeiwu_com6年前Python基础

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() 

以上这篇python保存数据到本地文件的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python入门_浅谈字符串的分片与索引、字符串的方法

这篇文章主要介绍了字符串的分片与索引、字符串的方法。 字符串的分片与索引: 字符串可以用过string[X]来分片与索引。分片,简言之,就是从字符串总拿出一部分,储存在另一个地方。 看下...

python障碍式期权定价公式

早期写的python障碍式期权的定价脚本,供大家参考,具体内容如下 #coding:utf-8 ''' 障碍期权 q=x/s H = h/x H 障碍价格 [1] Down-and-...

Python Numpy计算各类距离的方法

Python Numpy计算各类距离的方法

详细: 1.闵可夫斯基距离(Minkowski Distance) 2.欧氏距离(Euclidean Distance) 3.曼哈顿距离(Manhattan Distance) 4.切比...

深入分析python中整型不会溢出问题

本次分析基于 CPython 解释器,python3.x版本 在python2时代,整型有 int 类型和 long 长整型,长整型不存在溢出问题,即可以存放任意大小的整数。在pytho...

python opencv 二值化 计算白色像素点的实例

贴部分代码 #! /usr/bin/env python # -*- coding: utf-8 -*- import cv2 import numpy as np from PIL...