聊一聊python常用的编程模块


Posted in Python onMay 14, 2021

文件流的读写

读取保存数据为数组的txt文件

使用try进行异常发现,使用while检测文件末尾进行读取

file_to_read = raw_input("Enter file name of tests (empty string to end program):")
try:
    infile = open(file_to_read, 'r')
    while file_to_read != " ":
        file_to_write = raw_input("Enter output file name (.csv will be appended to it):")
        file_to_write = file_to_write + ".csv"
        outfile = open(file_to_write, "w")
        readings = (infile.readline())
        print readings
        while readings != 0:
            global count
            readings = int(readings)
            minimum = (infile.readline())
            maximum = (infile.readline())

使用for遍历读取的每一行,进行一次性的读取和输入

下面调用的程序读取的数据是

聊一聊python常用的编程模块

result = list()
    with open('../test/parameter.txt') as  f:
        for line in f.readlines():
            temp = list()
            # 逐个遍历对应每一行元素,将之转为对应的数据
            b = line.strip(",][").split(',')
            if(len(b) >= 5):
                b.pop()
            for a in b:
                a = a.replace('[','').replace(']','')
                temp.append(float(a))
            result.append(temp)
            #print("中途打印的temp是",temp)
            #print("加入到result中的结果是",result)

删除str中的特定字符

删除字符串首尾的多余字符串strip()

# 删除字符串中多余字符
def string_remove():
   str1 = ' abc     \n'
   print str1.strip()   # abc

   str2 = '----abcdf++++'
   print str2.strip('-+')  # abcdf

replace函数,删除字符串中某一个所有的字符串

ss = 'old old string'
ret = ss.replace('old', 'new', 1)
print(ret)

sub函数,同时删除多个字符串,这里使用了正则表达式

str2 = '\nabc\nwrt22\t666\t'  # 删除字符串中的所有\n,\t
import re
print(re.sub('[\n\t]','',str2))   # abcwrt22666

以上就是聊一聊python常用的编程模块的详细内容,更多关于python编程模块的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
python字符串替换示例
Apr 24 Python
python logging日志模块的详解
Oct 29 Python
python读取和保存视频文件
Apr 16 Python
python selenium 对浏览器标签页进行关闭和切换的方法
May 21 Python
python定时关机小脚本
Jun 20 Python
解读python如何实现决策树算法
Oct 11 Python
pandas dataframe添加表格框线输出的方法
Feb 08 Python
python画双y轴图像的示例代码
Jul 07 Python
使用PyTorch将文件夹下的图片分为训练集和验证集实例
Jan 08 Python
keras 多gpu并行运行案例
Jun 10 Python
Python 3.10 的首个 PEP 诞生,内置类型 zip() 迎来新特性(推荐)
Jul 03 Python
使用gunicorn部署django项目的问题
Dec 30 Python
如何获取numpy array前N个最大值
May 14 #Python
使用pandas模块实现数据的标准化操作
pandas 实现将NaN转换为None
May 14 #Python
Pandas||过滤缺失数据||pd.dropna()函数的用法说明
Python爬虫:从m3u8文件里提取小视频的正确操作
MATLAB 全景图切割及盒图显示的实现步骤
使用pandas或numpy处理数据中的空值(np.isnan()/pd.isnull())
May 14 #Python
You might like
Php中用PDO查询Mysql来避免SQL注入风险的方法
2013/04/25 PHP
阿里云PHP SMS短信服务验证码发送方法
2017/07/11 PHP
解决php用mysql方式连接数据库出现Deprecated报错问题
2019/12/25 PHP
JavaScript基本编码模式小结
2012/05/23 Javascript
js自执行函数的几种不同写法的比较
2012/08/16 Javascript
js 动态加载事件的几种方法总结
2013/12/25 Javascript
Jquery EasyUI中弹出确认对话框以及加载效果示例代码
2014/02/13 Javascript
javascript的propertyIsEnumerable()方法使用介绍
2014/04/09 Javascript
bootstrap3 兼容IE8浏览器!
2016/05/02 Javascript
vue实现留言板todolist功能
2017/08/16 Javascript
vue使用localStorage保存登录信息 适用于移动端、PC端
2019/05/27 Javascript
vue实现搜索功能
2019/05/28 Javascript
React中获取数据的3种方法及优缺点
2020/02/18 Javascript
vue-以文件流-blob-的形式-下载-导出文件操作
2020/08/07 Javascript
[02:20]2014DOTA2西雅图邀请赛 MVP外卡赛首胜采访
2014/07/09 DOTA
[04:02]DOTA2上海特锦赛小组赛第二日recap精彩回顾
2016/02/28 DOTA
[06:07]DOTA2-DPC中国联赛3月5日Recap集锦
2021/03/11 DOTA
Python操作Mysql实例代码教程在线版(查询手册)
2013/02/18 Python
python模拟登录百度贴吧(百度贴吧登录)实例
2013/12/18 Python
简单介绍Python的Django框架加载模版的方式
2015/07/20 Python
Python批量创建迅雷任务及创建多个文件
2016/02/13 Python
Python编程中对super函数的正确理解和用法解析
2016/07/02 Python
python闭包、深浅拷贝、垃圾回收、with语句知识点汇总
2020/03/11 Python
深入剖析webstorage[html5的本地数据处理]
2016/07/11 HTML / CSS
地球鞋加拿大官网:Earth Shoes Canada
2020/11/17 全球购物
求职信范文怎么写
2014/01/29 职场文书
气象学专业个人求职信
2014/03/15 职场文书
医院搬迁方案
2014/06/14 职场文书
社区党员志愿服务活动方案
2014/08/18 职场文书
解除劳动关系协议书范文
2014/09/11 职场文书
贵阳市党的群众路线教育实践活动党(工)委领导班子整改方案
2014/10/26 职场文书
2015年乡镇纪委工作总结
2015/05/26 职场文书
车辆管理制度范本
2015/08/05 职场文书
班级管理经验交流材料
2015/11/02 职场文书
2016春季幼儿园开学寄语
2015/12/03 职场文书
Python 机器学习工具包SKlearn的安装与使用
2021/05/14 Python