[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
方法一:open函数保存 #保存数据open函数 with open('D:/PythonWorkSpace/TestData/pinglun.txt','w'
,encoding='utf-8') as f:#使用with open()新建对象f for i in comments: print(i) f.write(
i+'\n')#写入数据,文件保存在上面指定的目录,加\n为了换行更方便阅读 方法二: numpy #导入包import pandas as pd
import numpy as np df = pd.DataFrame(np.random.randn(10,4))#创建随机值
#print(df.head(2))#查看数据框的头部数据,默认不写为前5行,小于5行时全部显示;也可以自定义查看几行 print(df.tail())
##查看数据框的尾部数据,默认不写为倒数5行,小于5行时全部显示;也可以自定义查看倒数几行 df.to_csv(
'D:/PythonWorkSpace/TestData/PandasNumpy.csv')#存储到CSV中
#df.to_excel('D:/PythonWorkSpace/TestData/PandasNumpy.xlsx')#存储到Excel中(需要提前导入库
pip install openpyxl) 方法三:csv写入 import csv import codecs with codecs.open(
'./test.csv', 'w', 'utf-8') as csvfile: # 指定 csv 文件的头部显示项 filednames = ['ID',
'PRICE'] writer = csv.DictWriter(csvfile, fieldnames=filednames)
writer.writeheader() for i in range(0, len(test_index)): try: writer.writerow({
'ID':test_index[i], 'PRICE':y_pred[i]}) except UnicodeEncodeError: print("编码错误,
该数据无法写到文件中, 直接忽略该数据") 方法四:DataFrame 可能的问题:csv文件中看不到数据,但是通过python代码可以看到数据
dataframe= pd.DataFrame({'ID':test_index,'PRICE': y_pred}) # dataframe =
pd.DataFrame({'PRICE': test_index}) dataframe.to_csv("test12.csv",index=
False,sep='\n') 方法五 import codecs #或者io,使用哪种包无所谓 with codecs.open(
'your_file.txt', 'r', 'utf-8') as f: f.write('This method is prior')