有关文本操作上的问题
本帖最后由 阿智兄 于 2021-3-28 03:11 编辑非常感谢你在百忙之中抽空为我解答。
以下这段代码是对DATA.TXT文本中冒号后面的数据进行分类的,输出的结果也只有冒号后面的那部分数据而已,现在想让它输出的结果中连同冒号前的那部分也一起输出(即A001:04,09,10,22,28,32),该何如改?再次感谢。
def check(n):
temp = int(n)//10+int(n)%10
if temp %2:
return '1'
else:
return '0'
def output(filename,content):
print(filename,content,'\n')
def output(filename,content):
f=open(filename,'w+')
for i in content:
f.write(','.join(i)+'\n')
f.write(data)
f.close()
f=open('data.txt','r+')
data=.split(',') for i in f]
f.close()
index1=
indexdict1=dict([] for i in set(index1)])
for i,j in zip(data,index1):
indexdict1.append(i)
for i in indexdict1:
output(''.join(i)+'.txt',indexdict1)
data.txt
A001:04,09,10,22,28,32
A002:09,10,19,25,26,29
A003:10,17,24,25,28,30
A004:01,07,15,16,20,23
A005:05,06,11,12,15,30
A006:06,08,11,22,25,33
A007:02,08,21,25,26,30
A008:01,20,23,26,27,32
A009:03,09,16,17,20,26
A010:10,15,17,27,29,31
A011:04,05,10,13,15,19
A012:01,06,12,18,22,24
A013:01,09,11,12,16,19
A014:02,21,23,26,31,32
A015:02,14,16,21,29,30
A016:01,06,12,26,29,30
A017:11,15,20,23,25,33
A018:02,04,06,15,24,27
A019:01,02,05,09,19,24
A020:03,07,16,17,23,30
A021:01,19,25,26,30,31
A022:02,08,11,17,21,30
A023:01,05,13,14,27,33
A024:14,15,18,22,31,33
A025:05,12,20,21,22,29
A026:03,11,14,16,21,32
A027:03,10,16,21,25,27
A028:10,15,16,18,20,27
A029:03,11,13,20,24,30
A030:04,08,09,13,19,33
A031:05,07,11,13,27,29
A032:06,08,10,15,17,26
A033:09,11,12,13,22,23
A034:01,02,04,06,19,21
A035:03,09,10,13,18,26
A036:12,16,21,26,27,32
A037:04,07,09,23,27,30
A038:02,09,13,17,26,28
A039:09,15,18,21,23,26
A040:01,03,07,21,27,32
A041:12,15,16,22,29,32
A042:10,14,17,22,26,27
A043:08,17,24,26,27,31
A044:05,09,14,20,24,30
A045:02,04,10,17,22,25
A046:01,03,11,12,19,26
A047:09,14,21,23,26,32
A048:02,05,08,12,26,31
A049:01,05,07,23,28,30
A050:03,10,19,25,26,31
本帖最后由 jackz007 于 2021-3-28 08:32 编辑
直接打印文件内容不行吗?
fp = open('data.txt')
for x in fp:
print(x . strip())
fp . close()
运行实况
D:\00.Excise\Python>python x.py
A001:04,09,10,22,28,32
A002:09,10,19,25,26,29
A003:10,17,24,25,28,30
A004:01,07,15,16,20,23
A005:05,06,11,12,15,30
A006:06,08,11,22,25,33
A007:02,08,21,25,26,30
A008:01,20,23,26,27,32
A009:03,09,16,17,20,26
A010:10,15,17,27,29,31
A011:04,05,10,13,15,19
A012:01,06,12,18,22,24
A013:01,09,11,12,16,19
A014:02,21,23,26,31,32
A015:02,14,16,21,29,30
A016:01,06,12,26,29,30
A017:11,15,20,23,25,33
A018:02,04,06,15,24,27
A019:01,02,05,09,19,24
A020:03,07,16,17,23,30
A021:01,19,25,26,30,31
A022:02,08,11,17,21,30
A023:01,05,13,14,27,33
A024:14,15,18,22,31,33
A025:05,12,20,21,22,29
A026:03,11,14,16,21,32
A027:03,10,16,21,25,27
A028:10,15,16,18,20,27
A029:03,11,13,20,24,30
A030:04,08,09,13,19,33
A031:05,07,11,13,27,29
A032:06,08,10,15,17,26
A033:09,11,12,13,22,23
A034:01,02,04,06,19,21
A035:03,09,10,13,18,26
A036:12,16,21,26,27,32
A037:04,07,09,23,27,30
A038:02,09,13,17,26,28
A039:09,15,18,21,23,26
A040:01,03,07,21,27,32
A041:12,15,16,22,29,32
A042:10,14,17,22,26,27
A043:08,17,24,26,27,31
A044:05,09,14,20,24,30
A045:02,04,10,17,22,25
A046:01,03,11,12,19,26
A047:09,14,21,23,26,32
A048:02,05,08,12,26,31
A049:01,05,07,23,28,30
A050:03,10,19,25,26,31
D:\00.Excise\Python> 看了半天代码也没看太明白你的意思
你是想根据冒号后边的6位数,生成一个六位数的二进制文件名,将内容写入是吗?
如果是根本不需要那么麻烦,很简单的,你完全可以
逐行读取 data.txt
然后 提取 冒号后 6 组数据
将 6组数据转换成 int
生成文件名.txt
写入 逐行读取的数据
不就结束了,哪有那么麻烦
你代码看的头疼,我重新敲一个,你看看是不是你想要那样
def check(n):
temp = int(n)//10+int(n)%10
if temp %2:
return '1'
else:
return '0'
def output(filename,content):
f=open(filename,'w+')
f.write(content)
f.close()
f=open('data.txt','r+')
data = f.readlines()
for i in data:
nums = list(map(int,i.split(":")[-1].split(",")))
filename = "".join()
output(filename + ".txt",i)
f.close()
jackz007 发表于 2021-3-28 08:28
直接打印文件内容不行吗?
运行实况
谢谢你的回复,代码的功能是对数据进行分类。所以不可以直接打印出来的。 本帖最后由 阿智兄 于 2021-3-28 11:44 编辑
qq1151985918 发表于 2021-3-28 08:39
看了半天代码也没看太明白你的意思
你是想根据冒号后边的6位数,生成一个六位数的二进制文件名,将内容写 ...
谢谢你的帮助,运行后得到的结果符合要求。
页:
[1]