Python将列表数据写入文件(txt, csv,excel)
作者:记不清下一秒 时间:2023-04-09 13:24:49
写入txt文件
def text_save(filename, data):#filename为写入CSV文件的路径,data为要写入数据列表.
file = open(filename,'a')
for i in range(len(data)):
s = str(data[i]).replace('[','').replace(']','')#去除[],这两行按数据不同,可以选择
s = s.replace("'",'').replace(',','') +'\n' #去除单引号,逗号,每行末尾追加换行符
file.write(s)
file.close()
print("保存文件成功")
写入csv文件
import csv
import codecs
def data_write_csv(file_name, datas):#file_name为写入CSV文件的路径,datas为要写入数据列表
file_csv = codecs.open(file_name,'w+','utf-8')#追加
writer = csv.writer(file_csv, delimiter=' ', quotechar=' ', quoting=csv.QUOTE_MINIMAL)
for data in datas:
writer.writerow(data)
print("保存文件成功,处理结束")
写入excel文件
# 将数据写入新文件
def data_write(file_path, datas):
f = xlwt.Workbook()
sheet1 = f.add_sheet(u'sheet1',cell_overwrite_ok=True) #创建sheet
#将数据写入第 i 行,第 j 列
i = 0
for data in datas:
for j in range(len(data)):
sheet1.write(i,j,data[j])
i = i + 1
f.save(file_path) #保存文件
以上所述是小编给大家介绍的Python将列表数据写入文件(txt, csv,excel)详解整合网站的支持!
来源:https://blog.csdn.net/u010513327/article/details/80889846
标签:Python,txt,csv,excel
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python入门教程2. 字符串基本操作【运算、格式化输出、常用函数】 <font color=red>原创</font>
2021-08-21 17:17:21
python爬取网页版QQ空间,生成各类图表
2023-01-18 06:30:58
![](https://img.aspxhome.com/file/2023/5/69025_0s.png)
python归并排序算法过程实例讲解
2023-12-02 23:28:06
![](https://img.aspxhome.com/file/2023/0/72170_0s.png)
Django 实现图片上传和下载功能
2023-01-14 09:53:21
![](https://img.aspxhome.com/file/2023/2/109442_0s.png)
Linux下 php7安装redis的方法
2024-05-13 09:54:05
![](https://img.aspxhome.com/file/2023/6/126226_0s.png)
MySQL 8.0新特性 — 管理端口的使用简介
2024-01-28 21:53:46
Python检查 云备份进程是否正常运行代码实例
2023-07-08 23:59:05
python自动生成model文件过程详解
2023-09-30 02:54:05
Python+Appium新手教程
2022-12-01 04:12:44
![](https://img.aspxhome.com/file/2023/4/77904_0s.png)
Python图像处理库PIL的ImageEnhance模块使用介绍
2023-05-14 17:29:51
![](https://img.aspxhome.com/file/2023/0/107650_0s.jpg)
撤回我也能看到!教你用Python制作微信防撤回脚本
2021-08-05 14:38:30
![](https://img.aspxhome.com/file/2023/9/90649_0s.png)
golang实现的文件上传下载小工具
2023-06-28 05:34:34
python实现分页效果
2021-07-12 16:00:44
![](https://img.aspxhome.com/file/2023/2/80552_0s.jpg)
PHP手机号码归属地查询代码(API接口/mysql)
2023-10-26 04:35:44
使用Python实现二分法查找的示例
2022-02-08 13:52:53
![](https://img.aspxhome.com/file/2023/9/85379_0s.png)
JS实现单击输入框弹出选择框效果完整实例
2024-04-22 22:33:02
![](https://img.aspxhome.com/file/2023/9/135649_0s.png)
20非常有用的Python单行代码分享
2021-07-21 07:16:57
Python中PyMySQL的基本操作
2024-01-15 20:57:11
用Python解决x的n次方问题
2022-02-02 06:30:10
pandas DataFrame 警告(SettingWithCopyWarning)的解决
2023-08-20 07:45:31