python实现将内容分行输出

yipeiwu_com5年前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()

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

相关文章

django 基于中间件实现限制ip频繁访问过程详解

额额,标题已经很醒目了,通过中间件去实现,其他方法也可以实现 浏览器前端传来的请求,必须通过中间件,才能到后面路由,视图函数,所以我们在中间件那里做一层处理,我们还需要知道是哪个ip,在...

python实现根据ip地址反向查找主机名称的方法

本文实例讲述了python实现根据ip地址反向查找主机名称的方法。分享给大家供大家参考。具体如下: import sys, socket try: result = socket...

详解python发送各类邮件的主要方法

 python中email模块使得处理邮件变得比较简单,今天着重学习了一下发送邮件的具体做法,这里写写自己的的心得,也请高手给些指点。 一、相关模块介绍 发送邮件主要用到了sm...

python在Windows下安装setuptools(easy_install工具)步骤详解

python在Windows下安装setuptools(easy_install工具)步骤详解

本文讲述了python在Windows下安装setuptools(easy_install工具)的方法。分享给大家供大家参考,具体如下: 【题外话介绍下setuptools】 setup...

Python如何获得百度统计API的数据并发送邮件示例代码

小工具 本来这么晚是不准备写博客的,当是想到了那个狗子绝对会在开学的时候跟我逼逼这个事情,所以,还是老老实实地写一下吧。 Baidu统计API的使用 系统环境: Python2...