Python基本操作

作者: Juan_NF | 来源:发表于2021-08-15 10:02 被阅读0次

1.文件读取与写入

  • with open(somefile) as f,是比较推荐的读取文件时的字段,可自动关闭文件,避免因为程序崩溃未及时写入;
  • 内置函数readlines,读取后,文件内容以列表的格式返回;
with open('D:/tmp/info.txt') as f:
    p1 = f.readlines()
    print(p1)
with open('D:/tmp/info1.txt',w) as f:
   f.writelines(p1)
  • read,读取后,文件内容以一个字符串的形式返回:
with open('D:/tmp/info.txt') as f:
    p2 = f.read()
    print(p2)
with open('D:/tmp/info2.txt',w) as f:
   f.writelines(p2)
  • pandas中的read_table函数和read_csv函数,pandas包对R用户比较友好,读取后为数据框,就相当于R中的read.table,read.csv函数;
import pandas as pd
with open('D:/tmp/info.txt') as f:
        p3 = pd.read_table(f)
        print(p3)
p3.to_csv('D:/tmp/info3.txt')

2.字符串操作

import re

a = ['Joey','Pheebe','Monica','Chandeler','Rachel','Ross']

####split和join的使用

b = ','.join(a)

'Hi '+a[0]
Out[78]: 'Hi Joey'

print(a)
['Joey', 'Pheebe', 'Monica', 'Chandeler', 'Rachel', 'Ross']

print(b)
Joey,Pheebe,Monica,Chandeler,Rachel,Ross

b.split(',')
Out[81]: ['Joey', 'Pheebe', 'Monica', 'Chandeler', 'Rachel', 'Ross']

#####判断字符串的有无,[]是模板的作用(?)

[re.search('Ph',x) for x in a]
Out[83]: [None, <re.Match object; span=(0, 2), match='Ph'>, None, None, None, None]

['Ph' in x for x in a]
Out[84]: [False, True, False, False, False, False]

#####字符串替换

seq =  'ACGTACCTA'
###table即所制定的变换的规则
table = str.maketrans('AT','TA')

seq.translate(table)
Out[88]: 'TCGATCCAT'

seq.replace('AC', 'TG')
Out[89]: 'TGGTTGCTA'

相关文章

  • Python基本操作

    1.文件读取与写入 with open(somefile) as f,是比较推荐的读取文件时的字段,可自动关闭文件...

  • python--文件的基本操作

    python编程中,文件是必不可少的。下面我们来学习python中文件的基本操作。 文件的基本操作: 在pytho...

  • Python 学习Day1

    Python 适合开发的领域 基本操作 函数 文件操作 参考 一、Python适合开发的领域 Web网站和各种网络...

  • Python字典基本操作

    希望对你有帮助,陌生人 字典的创建: phonebook = {'Alice':'2341','Beth':'91...

  • Python字典基本操作

    1 字典创建 (1) 直接创建 (2)通过dict先建立空字典,再添加值 (3) 通过列表创建字典 2 字典索引及...

  • python的基本操作

    一、python的六个标准的数据类型 Python3 的六个标准数据类型中: 不可变数据(3 个):Number(...

  • Python基础(十一)数据库编程

    1. 操作数据库的基本流程 2. Python操作数据库之sqlalchemy 详细操作可见以下链接Python操...

  • 2018-07-20小白的sqlmap学习

    一、基本操作 python sqlmap.py -u "url"#检测是否存在注入点 python sqlmap....

  • python--pandas切片

    pandas的切片操作是python中数据框的基本操作,用来选择数据框的子集。 环境 python3.9 win1...

  • day1-python3基础

    今天练习了python的基本知识。 1、数字类型的常规操作:math,random,numpy2、符串基本操作3、...

网友评论

    本文标题:Python基本操作

    本文链接:https://www.haomeiwen.com/subject/antubltx.html