python实现将内容分行输出

yipeiwu_com6年前Python基础

#python版一行内容分行输出
 

a="aA1一bB2二cC3三dD4四eE5五fF6六gG7七hH8八iI9九"
"""
分行输出为:
abcdefghi
ABCDEFGHI
123456789
一二三四五六七八九
"""
 
print("方法一:===============")
for r in range(0,4):
 t=''
 for s in range(0+r,len(a),4):
  t=t+a[s]
 print(t)
 
print("方法二:===============")
 
#=_=这个方法会不会看起来比较傻?
l=list(a)
ta=tb=tc=td=''
for r in range(0,9):
 for s in range(0,4):
  if s==0:
   ta=ta+l.pop(0)
  if s==1:
   tb=tb+l.pop(0)
  if s==2:
   tc=tc+l.pop(0)
  if s==3:
   td=td+l.pop(0)
print(ta)
print(tb)
print(tc)
print(td)
  
print("方法3:回字有N种写法===============")
import string
ta=tb=tc=td=''
la=string.ascii_lowercase
ua=string.ascii_uppercase
nb=string.digits
ub="一二三四五六七八九"
for s in a:
 if s in la:
  ta=ta+s
 if s in ua:
  tb=tb+s
 if s in nb:
  tc=tc+s
 if s in ub:
  td=td+s
print(ta)
print(tb)
print(tc)
print(td)
 
print("方法4:回字有一种叫做正则的写法===============")
import re
#这正则写法感觉不科学,暂时没有好的想法
reg=["[a-z]","[A-Z]","\d","[^\da-zA-Z]"]
for s in reg: 
 rega=re.compile(s)
 s=re.findall(rega,a)
 print("".join(s))
 
"""
输出:
方法一:===============
abcdefghi
ABCDEFGHI
123456789
一二三四五六七八九
方法二:===============
abcdefghi
ABCDEFGHI
123456789
一二三四五六七八九
方法3:回字有N种写法===============
abcdefghi
ABCDEFGHI
123456789
一二三四五六七八九
方法4:回字有一种叫做正则的写法===============
abcdefghi
ABCDEFGHI
123456789
一二三四五六七八九
"""

再给大家一个读取文件内容并分行输出的方法

f=open("shuju.txt","r")
content=f.read()
print content
for i in content:
  print i
f.close()
f=open('shuju.txt','w')
f.write(content)
f.close()

好了,小伙伴们自己好好研究下吧,很有意思。

相关文章

python中安装Scrapy模块依赖包汇总

本地虚拟环境开发完成之后,上线过程中需要一一安装依赖包,做个记录如下: CentOS 安装python3.5.3 wget https://www.python.org/ftp/py...

使用Python-OpenCV向图片添加噪声的实现(高斯噪声、椒盐噪声)

在matlab中,存在执行直接得函数来添加高斯噪声和椒盐噪声。Python-OpenCV中虽然不存在直接得函数,但是很容易使用相关的函数来实现。 代码: import numpy a...

Python OrderedDict的使用案例解析

这篇文章主要介绍了Python OrderedDict的使用案例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 很多人认为python...

python 读取.csv文件数据到数组(矩阵)的实例讲解

利用numpy库 (缺点:有缺失值就无法读取) 读: import numpy my_matrix = numpy.loadtxt(open("1.csv","rb"),delim...

Python中获取对象信息的方法

当我们拿到一个对象的引用时,如何知道这个对象是什么类型、有哪些方法呢? 使用type() 首先,我们来判断对象类型,使用type()函数: 基本类型都可以用type()判断: >...