标签:ast 入参 trace 返回值 rac 方法 通用 程序 使用方法
项目: 51备忘录v0.32添加功能:
#保存数据,纯文本,pkl
data = [{‘id‘:1, ‘time‘:‘1.2‘, ‘thing‘:‘买了一件毛衣‘}, {‘id‘:2, ‘time‘:‘1.6‘, ‘thing‘:‘find a bug‘}, {‘id‘:3, ‘time‘:‘1.3‘, ‘thing‘:‘write five lines of python‘}]
import os
base_dir = r‘D:\python全站‘ #基准目录
data_path = os.path.join(base_dir, ‘data.db‘) # 文件的目录,os.path.join()组合目录
with open(data_path, ‘w‘) as f:
for db in data:
print(db.values())
line = ‘-‘.join(db.values()) # 出现下面错误,是因为字典的值的类型不一样,有int,有str,需要统一类型
dict_values([1, ‘1.2‘, ‘买了一件毛衣‘])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-32-4092e5ac35cc> in <module>()
9 for db in data:
10 print(db.values())
---> 11 line = ‘-‘.join(db.values())
12
TypeError: sequence item 0: expected str instance, int found
# open? # open的使用方法和参数,此次用到的是encoding=‘utf-8‘
# Signature: open(file, mode=‘r‘, buffering=-1, encoding=None,
errors=None, newline=None,
closefd=True, opener=None)
#保存数据,纯文本,pkl
data = [{‘id‘:1, ‘time‘:‘1.2‘, ‘thing‘:‘买了一件毛衣‘}, {‘id‘:2, ‘time‘:‘1.6‘, ‘thing‘:‘find a bug‘}, {‘id‘:3, ‘time‘:‘1.3‘, ‘thing‘:‘write five lines of python‘}]
import os
base_dir = r‘D:\python全站‘
data_path = os.path.join(base_dir, ‘data.db‘)
with open(data_path, ‘w‘) as f:
for db in data:
print(db.values())
line = ‘-‘.join([str(x) for x in db.values()]) # 列表推导式[x for x in 可迭代的数据结构]
f.write(line) # 将处理过的数据写入到文件f,data_path
dict_values([1, ‘1.2‘, ‘买了一件毛衣‘])
dict_values([2, ‘1.6‘, ‘find a bug‘])
dict_values([3, ‘1.3‘, ‘write five lines of python‘])
# 1-1.2-?ňá?????????2-1.6-find a bug3-1.3-write five lines of python # 没有换行的操作:
#保存数据,纯文本,pkl
data = [{‘id‘:1, ‘time‘:‘1.2‘, ‘thing‘:‘买了一件毛衣‘}, {‘id‘:2, ‘time‘:‘1.6‘, ‘thing‘:‘find a bug‘}, {‘id‘:3, ‘time‘:‘1.3‘, ‘thing‘:‘write five lines of python‘}]
import os
base_dir = r‘D:\python全站‘
data_path = os.path.join(base_dir, ‘data.db‘)
with open(data_path, ‘w‘) as f:
for db in data:
print(db.values())
line = ‘-‘.join([str(x) for x in db.values()]) # 列表推导式
f.write(line + ‘\n‘) # 换行的操作 + ‘\n‘
dict_values([1, ‘1.2‘, ‘买了一件毛衣‘])
dict_values([2, ‘1.6‘, ‘find a bug‘])
dict_values([3, ‘1.3‘, ‘write five lines of python‘])
#保存数据,纯文本,pkl
data = [{‘id‘:1, ‘time‘:‘1.2‘, ‘thing‘:‘买了一件毛衣‘}, {‘id‘:2, ‘time‘:‘1.6‘, ‘thing‘:‘find a bug‘}, {‘id‘:3, ‘time‘:‘1.3‘, ‘thing‘:‘write five lines of python‘}]
import os
base_dir = r‘D:\python全站‘
data_path = os.path.join(base_dir, ‘data.db‘)
with open(data_path, ‘w‘, encoding = ‘utf-8‘) as f: # 写数据时候使用的编码是 utf-8,通用
for db in data:
print(db.values())
line = ‘-‘.join([str(x) for x in db.values()]) # 列表推导式
f.write(line + ‘\n‘)
dict_values([1, ‘1.2‘, ‘买了一件毛衣‘])
dict_values([2, ‘1.6‘, ‘find a bug‘])
dict_values([3, ‘1.3‘, ‘write five lines of python‘])
#保存数据,纯文本,pkl 封装
def save_dic(dic):
# import os
# base_dir = r‘D:\python全站‘
# data_path = os.path.join(base_dir, ‘data.db‘)
with open(data_path, ‘w‘, encoding = ‘utf-8‘) as f:
for db in data:
print(db.values())
line = ‘-‘.join([str(x) for x in db.values()]) # 列表推导式
f.write(line + ‘\n‘)
return ‘saved‘ # 思想:程序正确执行与否,都返回一个状态码。
save_dic(data_path) # 调用函数,传入参数
dict_values([1, ‘1.2‘, ‘买了一件毛衣‘])
dict_values([2, ‘1.6‘, ‘find a bug‘])
dict_values([3, ‘1.3‘, ‘write five lines of python‘])
‘saved‘
#保存数据,纯文本,pkl 封装 + 异常判断 + 格式化返回值
def save_dic(dic):
‘保持数据到文件‘
ret = {‘status‘:0, ‘statusText‘:‘saved‘}
try:
with open(data_path, ‘w‘, encoding = ‘utf-8‘) as f:
for db in data:
# print(db.values())
line = ‘-‘.join([x for x in db.values()]) # x没有转换
f.write(line + ‘\n‘)
except Exception as err:
# print(err)
ret[‘status‘] = 1
ret[‘statusText‘] = err
return ret
# save_dic(data)
save_dic(data_path)
{‘status‘: 1,
‘statusText‘: TypeError(‘sequence item 0: expected str instance, int found‘)}
#保存数据,纯文本,pkl 封装 + 异常判断 + 格式化返回值
def save_dic(dic):
‘保持数据到文件‘
ret = {‘status‘:0, ‘statusText‘:‘saved‘}
try:
with open(data_path, ‘w‘, encoding = ‘utf-8‘) as f:
for db in data:
# print(db.values())
line = ‘-‘.join([str(x) for x in db.values()]) #
f.write(line + ‘\n‘)
except Exception as err:
# print(err)
ret[‘status‘] = 1
ret[‘statusText‘] = err
return ret
# save_dic(data)
save_dic(data_path)
{‘status‘: 0, ‘statusText‘: ‘saved‘}
import pickle
pkl_path = os.path.join(base_dir, ‘data.pkl‘)
with open(pkl_path, ‘wb‘) as f: # 如果没有这个文件的话,pickle会自动创建这个文件
pickle.dump(data, f)
def save_to_pkl(dic):
pkl_path = os.path.join(base_dir, ‘data.pkl2‘)
with open(pkl_path, ‘wb‘) as f: # 使用wb模式,保存
pickle.dump(dic, f)
save_to_pkl(data)
标签:ast 入参 trace 返回值 rac 方法 通用 程序 使用方法
原文地址:http://blog.51cto.com/13118411/2140076