用python提取txt文件中的特定信息并写入Excel
2024-09-08 18:42:52
这个是用 excel里面的 去掉空格最后导出的一个list:
原本是有空格的
后面是抵消了中间的空格。
然后 这里侧重说一下什么是split()函数
语法:str.split(str="",num=string.count(str))[n]
参数说明:
str:表示为分隔符,默认为空格,但是不能为空(‘’)。若字符串没有分隔符,则把整个字符串作为列表的一个元素。
num:表示分割次数。如果参在参数num, 则仅分隔成num+1个字符串,并且每一个字符串可以赋给新的变量。
[n]:表示选取第n个分片
举个栗子:
str = "Line1-abcdef \nLine2-abc \nLine4-abcd"
print(str.split())
然后导出来的就是分隔符
后面可以写数字 1,意思是分隔成两个参数
还有一个栗子:
就是以#号为分隔符,指定第二个参数为1,其实就是分隔成两个参数列表。
当前目录;
import os
import xlwt
a = os.getcwd() #获取当前目录
print(a)#打印当前目录
os.chdir(r'C:\Users\yjiang3\Documents\YANNI JIANG')
print(a)
with open('11.txt','r') as raw:
for line in raw:
print(line)
with open('11.txt','r',encoding = 'utf-8') as fr, open('10.txt','w',encoding='utf-8') as fd:
for text in fr.readlines():
if text.split():
fd.write(text)
print('success') book = xlwt.Workbook(encoding='utf-8',style_compression=0)
sheet = book.add_sheet('10', cell_overwrite_ok=True) sheet.write(0, 0, 'x')
sheet.write(0, 1, 'Y') n=1
with open('10.txt','r+') as fd:
for text in fd.readlines():
x=text.split()[0]
y=text.split()[0] print(x.split('w'))
print(y.split('w')) sheet.write(n,0,x.split('w')[0])
sheet.write(n,1,y.split('w')[0]) n = n+1 book.save(r'C:\Users\yjiang3\Documents\YANNI JIANG\10.xls')
最新文章
- Android Weekly Notes Issue #233
- bzoj2141 树状数组套Treap树
- C#并行编程-线程同步原语
- 找到SQL Server数据库历史增长信息
- ASP.NET MVCでResponse Headerのサーバーバージョンをどうやって隠しますか?
- Object C学习笔记13-Dictionary字典
- 利用URLRewriter.dll 实现ASP.NET实现伪静态
- javascript form验证、完善 第24节
- 一步步学习ASP.NET MVC3 (6)——@helper,@functions
- 知识点整理之Java的Cookie操作
- 我和ASP.NET MVC有个约会
- TWRP-recovery中文界面安装方法[转]
- tomcat------https单向认证和双向认证
- 实现简答LinkedList
- 单例模式 GetInstance()
- CSipSimple最新版本号
- C++ enum用法小技巧
- 实验吧 deeeeeeaaaaaadbeeeeeeeeeef-20
- iOS webview 获取html中的图片地址
- [工作积累] UE4 并行渲染的同步 - Sync between FParallelCommandListSet &; FRHICommandListImmediate calls
热门文章
- yolov5查看训练日志图片和直方图(包括稀疏训练bn直方图)
- python exec_command 命令无效的原因
- Shiro 身份认证绕过漏洞 CVE-2022-32532
- c++ 引用 指针。
- Vue3学习笔记
- yii框架中 不能正确正常使用phpredis 路径安装好后还是报 Class yii\redis\Connection does not exist!
- 87、linux root 密码忘记了
- vue中标签的替换以及scoped实现css对当前文件起作用的原理
- git stash (pycharm/vscode的gui演示)
- 三种将list转换为map的方法