标题:[求助]有关文本操作上的问题
只看楼主
阿智兄
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-3-8
结帖率:87.5%
已结贴  问题点数:20 回复次数:2 
[求助]有关文本操作上的问题
非常感谢你在百忙之中抽空为我解答。
以下这段代码是对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=[i.strip().split(":")[1].split(',') for i in f]
f.close()

index1=[tuple(map(lambda x:check1(x),i)) for i in data]
indexdict1=dict([[i,[]] for i in set(index1)])
for i,j in zip(data,index1):
    indexdict1[j].append(i)
for i in indexdict1:
    output(''.join(i)+'.txt',indexdict1[i])


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

[此贴子已经被作者于2021-3-28 03:10编辑过]

搜索更多相关主题的帖子: data for 文本 content txt 
2021-03-28 03:03
zyb159357
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:25
专家分:113
注 册:2021-3-15
得分:20 
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,data):
    f=open(filename,'w+')   
    lst=[i for i in content]
    for d in data:      
        if  d.strip().split(":")[1].split(",") in lst:
            f.write(d)
    f.close()
        
f=open('data.txt','r+')
file_data=f.readlines() #保留原数据.
data=[i.strip().split(":")[1].split(',') for i in file_data]
f.close()

index1=[tuple(map(lambda x:check(x),i)) for i in data]
indexdict1=dict([[i,[]] for i in set(index1)])
for i,j in zip(data,index1):
    indexdict1[j].append(i)
for i in indexdict1:
    output(''.join(i)+'.txt', indexdict1[i], file_data)

2021-03-28 09:04
阿智兄
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-3-8
得分:0 
回复 2楼 zyb159357
非常感谢你的帮助,完全符合要求。谢谢。
2021-03-28 11:32



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-505271-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.225776 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved