❶ 用python读取文本文件,对读出的每一行进行操作,这个怎么写
用python读取文本文件,对读出的每一行进行操作,写法如下:
f=open("test.txt","r")
whileTrue:
line=f.readline()
ifline:
pass#dosomethinghere
line=line.strip()
p=line.rfind('.')
filename=line[0:p]
print"create%s"%line
else:
break
f.close()
❷ 用python读取.txt文件内容,去除空行和注释行后,以行为单位进行排序,并将结果输出为新的.txt文件
a=open("cdays-4-test.txt","r")
_a=open("cdays-4-result.txt","w")
for b in a:
if b.startswith("#"):
continue
c=b.split()
if len(c)==0:
continue
else:
_a.write(b)
a.close()
_a.close()
你程序中间为啥要运行一次result.sort()呢?
你吧你的result.sort()去掉就正确了....你要求严格排序的话就不能去改顺序.....
你在那个result.sort()之前加一个print result 可以更好的看到在调用这个方法前后的不同之处......
我的那个是另一种写法 个人习惯写法问题...不用看 或者可以稍微了解一下处理过程...
❸ Python对文本里面的内容排序
把文本的格式贴出来看看
假设格式是:
5KB
100KB
1MB
3MB
9MB
2MB
可用下面的程序(python3)
units={"KB":1,"MB":1024}
defcalc(x):
forunit,amountinunits.items():
ifx.find(unit)>0:
number=int(x[:-2])
number*=amount
returnnumber
file=open("sort.txt","r")
values=list(file)
file.close()
values=[value[:-1]forvalueinvalues]
values.sort(key=calc)
print(values)
values=[value+" "forvalueinvalues]
file=open("sort.txt","w")
file.write("".join(values))
file.close()
❹ python 给文档内部数字排序
#-*-coding:utf-8-*-
defsortdata(file1,file2):
data=open(file1,'r').read()
num=data.split('')
#正序
data1=sorted(num)
#倒序
#data2=sorted(num,reverse=True)
f=open(file2,'a')
foriindata1:
f.write(i+'')
f.close()
sortdata('word1.txt','word2.txt')
❺ python如何将一个文本里得学生姓名和成绩按顺序读取到另一个文件
python如何将一个文本里得学生姓名和成绩按顺序读取到另一个文件 python如何将一个文本里得学生姓名和成绩按顺序读取到另一个文件
❻ python读入一个txt文档,格式如xxx:123 yyy:321 ccc:777 怎样按照数字大小进行排序,输出前面字母和数字
s = open("一个txt文档").read()
sorted(s.split(), key = lambda s:int(s.split(':')[1]))
❼ 怎样用python对一个txt文件的所有行做重新排列
open打开,逐行加入到list里,用list自带的排序方法排序,然后按下标逐行写回到文件
❽ 在python里,怎么实现读入一个文档,将里面的单词排序,顺序就像英语字典里面的一样
在python里,怎么实现读入一个文档,将里面的单词排序,顺序就像英语字典里面的一样
❾ Python实现txt文件读取数字,排序,求均值