聊一聊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 相关文章推荐
wxPython窗口的继承机制实例分析
Sep 28 Python
python循环监控远程端口的方法
Mar 14 Python
利用Python的Flask框架来构建一个简单的数字商品支付解决方案
Mar 31 Python
python集合类型用法分析
Apr 08 Python
Python六大开源框架对比
Oct 19 Python
Python多进程同步简单实现代码
Apr 27 Python
python爬取w3shcool的JQuery课程并且保存到本地
Apr 06 Python
学Python 3的理由和必要性
Nov 19 Python
flask 实现上传图片并缩放作为头像的例子
Jan 09 Python
python适合做数据挖掘吗
Jun 16 Python
Kears 使用:通过回调函数保存最佳准确率下的模型操作
Jun 17 Python
Python运算符+与+=的方法实例
Feb 18 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操作SVN版本服务器类代码
2011/11/27 PHP
gd库图片下载类实现下载网页所有图片的php代码
2012/08/20 PHP
PHP排序算法类实例
2015/06/17 PHP
PHP单例模式详解及实例代码
2016/12/21 PHP
PHP排序算法之冒泡排序(Bubble Sort)实现方法详解
2018/04/20 PHP
Ctrl+Enter提交内容信息
2006/06/26 Javascript
基于jquery的下拉框改变动态添加和删除表格实现代码
2020/09/12 Javascript
window.dialogArguments 使用说明
2011/04/11 Javascript
document.getElementById介绍
2011/09/13 Javascript
鼠标拖拽移动子窗体的JS实现
2014/02/25 Javascript
javascript实现设置、获取和删除Cookie的方法
2015/06/01 Javascript
深入学习jQuery Validate表单验证
2016/01/18 Javascript
基于canvas实现的绚丽圆圈效果完整实例
2016/01/26 Javascript
关于javascript获取内联样式与嵌入式样式的实例
2017/06/01 Javascript
AngularJs+Bootstrap实现漂亮的计算器
2017/08/10 Javascript
layui table动态表头 改变表格头部 重新加载表格的方法
2019/09/21 Javascript
放弃 Python 转向 Go语言有人给出了 9 大理由
2017/10/20 Python
获取python文件扩展名和文件名方法
2018/02/02 Python
详细介绍Python进度条tqdm的使用
2019/07/31 Python
python创建与遍历List二维列表的方法
2019/08/16 Python
在notepad++中实现直接运行python代码
2019/12/18 Python
Python读取文件内容为字符串的方法(多种方法详解)
2020/03/04 Python
Python configparser模块封装及构造配置文件
2020/08/07 Python
Trip.com香港网站:Ctrip携程旗下,全球最大的网上旅游社之一
2016/08/01 全球购物
ALLSAINTS英国官网:伦敦新锐潮流品牌
2016/09/19 全球购物
毕业生毕业总结的自我评价范文
2013/11/02 职场文书
求职自荐信格式
2013/12/04 职场文书
初中女生自我鉴定
2013/12/19 职场文书
工作表扬信的范文
2014/01/10 职场文书
企业管理培训感言
2014/01/27 职场文书
《春笋》教学反思
2014/04/15 职场文书
博士生导师推荐信
2014/07/08 职场文书
旷工检讨书1000字
2015/01/01 职场文书
贷款担保书
2015/01/20 职场文书
2015年度员工自我评价范文
2015/03/11 职场文书
2015银行年终工作总结范文
2015/05/26 职场文书