聊一聊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 08 Python
深入讲解Python中面向对象编程的相关知识
May 25 Python
python urllib爬取百度云连接的实例代码
Jun 19 Python
windows下python 3.6.4安装配置图文教程
Aug 21 Python
Django如何开发简单的查询接口详解
May 17 Python
django-初始配置(纯手写)详解
Jul 30 Python
详解Python 中sys.stdin.readline()的用法
Sep 12 Python
Python基于内置库pytesseract实现图片验证码识别功能
Feb 24 Python
什么是Python包的循环导入
Sep 08 Python
Python实现AES加密,解密的两种方法
Oct 03 Python
Python如何把不同类型数据的json序列化
Apr 30 Python
yolov5返回坐标的方法实例
Mar 17 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 5.5版本的array_column()函数
2014/10/24 PHP
php判断访问IP的方法
2015/06/19 PHP
thinkPHP内置字符串截取函数用法详解
2016/11/15 PHP
thinkPHP5框架中widget的功能与用法详解
2018/06/11 PHP
深入解读JavaScript中的Hoisting机制
2015/08/12 Javascript
AngularJS Bootstrap详细介绍及实例代码
2016/07/28 Javascript
如何理解jQuery中的ajaxSubmit方法
2017/03/13 Javascript
js实现按座位号抽奖
2017/04/05 Javascript
JS中判断字符串存在和非空的方法
2018/09/12 Javascript
vue中如何实现后台管理系统的权限控制的方法步骤
2019/09/05 Javascript
[04:52]DOTA2亚洲邀请赛附加赛 TOP10精彩集锦
2015/01/29 DOTA
Python实现处理管道的方法
2015/06/04 Python
Python+Opencv识别两张相似图片
2020/03/23 Python
Python中工作日类库Busines Holiday的介绍与使用
2017/07/06 Python
Request的中断和ErrorHandler实例解析
2018/02/12 Python
Python cookbook(数据结构与算法)实现查找两个字典相同点的方法
2018/02/18 Python
Python绘制3D图形
2018/05/03 Python
python中使用iterrows()对dataframe进行遍历的实例
2018/06/09 Python
使用Python处理BAM的方法
2018/09/28 Python
python中将两组数据放在一起按照某一固定顺序shuffle的实例
2019/07/15 Python
python3实现的zip格式压缩文件夹操作示例
2019/08/17 Python
利用python绘制数据曲线图的实现
2020/04/09 Python
如何基于Python Matplotlib实现网格动画
2020/07/20 Python
会计自我鉴定
2013/11/02 职场文书
绿化先进工作者事迹材料
2014/01/30 职场文书
简历中的自我评价范文
2014/02/05 职场文书
销售员岗位职责
2015/02/10 职场文书
2015年业务工作总结范文
2015/04/10 职场文书
财务稽核岗位职责
2015/04/13 职场文书
政审证明范文
2015/06/19 职场文书
2016年中学法制宣传日活动总结
2016/04/01 职场文书
浅谈Python列表嵌套字典转化的问题
2021/04/07 Python
关于maven依赖 ${xxx.version}报错问题
2022/01/18 Java/Android
一条 SQL 语句执行过程
2022/03/17 MySQL
Spring Boot 底层原理基础深度解析
2022/04/03 Java/Android
使用 DataAnt 监控 Apache APISIX的原理解析
2022/07/07 Servers