python 常用读写操作
python读写操作
SYSTEM_STATUS = True if "windows" in platform.platform().lower() else False # 操作系统
if SYSTEM_STATUS:
txt_file_name = os.getcwd() + '\spudd.txt'
else:
txt_file_name = os.getcwd() + '/spudd.txt'
# 读取文件内容 # 301161,35843
with open(txt_file_name, 'r', encoding='utf8') as fr:
# 按行读取
for Id in fr.readlines():
# 逗号切割
spuId = Id.split(",")[1]
xiaoId = Id.split(",")[0]
details = sh.get_details_content(spuId)
imgList = details.get("imgList")
dict_str ={
xiaoId:imgList
}
# 追加写入文件
with open("img.text", 'a+', encoding='utf8') as fr:
fr.write(str(dict_str) + "\n")
字典dict强转成字符串 str
<class 'dict'> 换行写入txt文件
with open('register_aa.txt', 'a+',encoding='utf-8') as f:
f.write(str(phone_dict) + "\n")