python-从文本文件中删除字符串保持浮动
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-从文本文件中删除字符串保持浮动,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2335字,纯文字阅读大概需要4分钟。
内容图文
![python-从文本文件中删除字符串保持浮动](/upload/InfoBanner/zyjiaocheng/676/b1813190d70b4e2cb30a06b8a64880f1.jpg)
我正在寻找在文本文件中删除带有字符串的行或空行.看起来像这样.如您所见,标题在整个文件中自行重复.每个数据块的数据行数不同.我需要将其导入为numpy中的数组.起初,我用逗号表示小数点至少可以更改它.
我试过了,但是根本不起作用:
from types import StringType
z = open('D:\Desktop\cycle 1-20 20-50 kPa (dot).dat', 'r')
for line in z.readlines():
for x in z:
if type(z.readline(x)) is StringType:
print line
z.close()
数据示例:
bla bla
cyclical stuff Time: 81.095947 Sec 2012-08-02 17:05:42
stored : 1 cycle stores for : 62 seg-cycle
Points : 4223
Servo_Hyd count Temps Servo_Air pressure Servo_Hyd load Servo_Hyd LVDT1 Servo_Hyd LVDT2 Servo_Hyd LVDT3
name1 name1 name1 name1 name1 name1 name1
1 60.102783 0.020013755 89.109558 0.3552089 0.4015148 -0.33822596
1 60.107666 0.020006953 89.025749 0.35519764 0.4015218 -0.33821729
1 60.112549 0.02000189 88.886292 0.3551946 0.4015184 -0.33822691
1 60.117432 0.020007374 89.559196 0.35519707 0.40151948 -0.33823174
1 60.122314 0.019991774 89.741402 0.35519552 0.40151322 -0.33822927
1 60.127197 0.020003742 89.748924 0.35520011 0.40150556 -0.33822462
bla bla
cyclical stuff Time: 81.095947 Sec 2012-08-02 17:05:42
stored : 1 cycle stores for : 62 seg-cycle
Points : 4223
Servo_Hyd count Temps Servo_Air pressure Servo_Hyd load Servo_Hyd LVDT1 Servo_Hyd LVDT2 Servo_Hyd LVDT3
name1 name1 name1 name1 name1 name1 name1
1 60.102783 0.020013755 89.109558 0.3552089 0.4015148 -0.33822596
1 60.107666 0.020006953 89.025749 0.35519764 0.4015218 -0.33821729
1 60.112549 0.02000189 88.886292 0.3551946 0.4015184 -0.33822691
1 60.117432 0.020007374 89.559196 0.35519707 0.40151948 -0.33823174
1 60.122314 0.019991774 89.741402 0.35519552 0.40151322 -0.33822927
1 60.127197 0.020003742 89.748924 0.35520011 0.40150556 -0.33822462
解决方法:
Python最初将所有文件元素都读取为字符串,除非您强制转换它们,否则您的方法将无效.
最好的选择可能是使用正则表达式来过滤掉其中包含非数据字符的行.
f = open("datafile")
for line in f:
#Catch everything that has a non-number/space in it
if re.search("[^-0-9.\s]",line):
continue
# Catch empty lines
if len(line.strip()) == 0:
continue
# Keep the rest
print(line)
f.close()
内容总结
以上是互联网集市为您收集整理的python-从文本文件中删除字符串保持浮动全部内容,希望文章能够帮你解决python-从文本文件中删除字符串保持浮动所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。