将列表分组为特定的组

时间:2019-11-13 10:35:35

标签: python python-3.x grouping listings

我想知道是否可以将清单转换为特定的组,以后再以表格格式将其放置到这些组中。

这是我需要分组的输出,我将它们转换为列表,以便可以轻松地以表格方式对其进行划分。

f=open("sample1.txt", "r")
f.read()

以下是输出:

'0245984300999992018010100004+14650+121050FM-12+004699999V0203001N00101090001CN008000199+02141+01971101171ADDAY141021AY241021GA1021+006001081GA2061+090001021GE19MSL   +99999+99999GF106991021999006001999999KA1120N+02111MD1210141+9999MW1051REMSYN10498430 31558 63001 10214 20197 40117 52014 70544 82108 333 20211 55062 56999 59012 82820 86280 555 60973=\n'

这是我已经做的。我设法将其更改为一个列表,该列表产生了以下输出:

with open('sample1.txt', 'r') as file:
data = file.read().replace('\n', '')
print (list(data))

输出:

['0','2','4','5','9','8','4','3','0','0','9','9 ','9','9','9','2','0','1','8','0','1','0','1','0', '0','0','0','4','+','1','4','6','5','0','+','1','2 ','1','0','5','0','F','M','-','1','2','+','0','0', '4','6','9','9','9','9','9','V','0','2','0','3','0 ','0','1','N','0','0','1','0','1','0','9','0','0', '0','1','C','N','0','0','8','0','0','0','1','9','9 ','+','0','2','1','4','1','+','0','1','9','7','1', '1','0','1','1','7','1','A','D','D','A','Y','1','4 ','1','0','2','1','A','Y','2','4','1','0','2','1', 'G','A','1','0','2','1','+','0','0','6','0','0','1 ','0','8','1','G','A','2','0','6','1','+','0','9', '0','0','0','1','0','2','1','G','E','1','9','M','S' ','L',',',',',','+','9','9','9','9','9','+','9','9' ,“ 9”,“ 9”,“ 9”,“ G”,“ F”,“ 1”,“ 0”,“ 6”,“ 9”,“ 9”,“ 1”,“ 0”,“ 2”,“ 1”,“ 9”,“ 9”,“ 9”,“ 0”,“ 0”,“ 6”,“ 0”,“ 0”,“ 1”,“ 9”,“ 9”,“ 9”,“ 9”,“ 9”,“ 9”,“ K”,“ A”,“ 1”,“ 1”,“ 2”,“ 0”,“ N”,“ +” ,'0','2','1','1','1','M','D','1','2','1','0','1',' 4','1','+','9','9','9','9','M','W','1','0','5','1' ,“ R”,“ E”,“ M”,“ S”,“ Y”,“ N”,“ 1”,“ 0”,“ 4”,“ 9”,“ 8”,“ 4”,“ 3','0','','3','1','5','5','8',','6','3','0','0',' 1',',','1','0','2','1','4','','2','0','1','9','7',' ','4','0','1','1','7',',','5','2','0','1','4',',','7 ','0','5','4','4','','8','2','1','0','8',','3','3 ','3','','2','0','2','1','1',','5','5','0','6','2 ”,“”,“ 5”,“ 6”,“ 9”,“ 9”,“ 9”,“”,“ 5”,“ 9”,“ 0”,“ 1”,“ 2”,“” ,'8','2','8','2','0',',','8','6','2','8','0',',','5 ,“ 5”,“ 5”,“”,“ 6”,“ 0”,“ 9”,“ 7”,“ 3”,“ =”]

我的目标是将它们分为以下几类:

0245,984300,99999,2018,01,01,0000,4,+1.... 

属于每一列的位数是预先确定的,例如,第一列总有4位,第二列总有6位,依此类推。

我正在考虑将它们串联起来。但是我不确定是否可能。

2 个答案:

答案 0 :(得分:2)

您可以使用[ H|013450107776|10/15/2019,D|TXN001|10/15/2019|013450107806|LCUATADA05|1000.00|PAYMENT FOR SERVICE|Successful,D|TXN002|10/15/2019|013450107806|LCUATADA05|1000.00|PAYMENT FOR SERVICE|Successful,D|TXN002|10/15/2019|013458001581|LCUATADA04|1500.00|INVOICE 001|Successful, D|TXN003|10/15/2019|013450107784|LCUATADA01|1750.00|SEPT PAYMENT|Successful,H|007442500211|11/05/2019,D|1000000489|007442500211|0009204332|85585.44|SEPTPAYMENT|Successful...] How i can form the string value(header information followed by detail information) like "H|013450207776|10/15/2019 D|0000TXN001|10/15/2019|013450107806|LCUATADA05|1000.00|PAYMENT FOR SERVICE|Successful" from the List

operator.itemgetter

甚至更好的是,您可以使用zip和from operator import itemgetter g = itemgetter(slice(0, 4), slice(4, 10)) with open('sample1.txt') as file: for line in file: print(g(line)) 动态制作切片:

itertools.accumulate

然后像以前一样

答案 1 :(得分:1)

如果您确实想使用此数据,我建议命名所有内容,并仔细检查所有长度是否有意义。所以开始吧

with open('sample1.txt', 'r') as file:
    data = file.read().rstrip('\n"')
    first, second, *rest = data.split()

    if len(first) != 163:
        raise ValueError(f"The first part should be 163 characters long, but it's {len(first)}")
    if len(second) != 163:
        raise ValueError(f"The second part should be  characters long, but it's {len(first)}")

所以现在您有3个变量

  • first"0245984300999992018010100004+14650+121050FM-12+004699999V0203001N00101090001CN008000199+02141+01971101171ADDAY141021AY241021GA1021+006001081GA2061+090001021GE19MSL"
  • second"+99999+99999GF106991021999006001999999KA1120N+02111MD1210141+9999MW1051REMSYN10498430"
  • rest['31558', '63001', '10214', '20197', '40117', '52014', '70544', '82108', '333', '20211', '55062', '56999', '59012', '82820', '86280', '555', '60973']

然后重复这个想法

date, whatever, whatever2, whatever3 = first.split('+')

然后解析第一部分,我将得到一个类似

的列表
something = date[0:4]
something_else = date[4:10]
third_thing = date[10:15]
year = [15:19]
month = [19:21]
day = [21:23]

,依此类推。然后,您可以在分析它们的代码中使用所有这些变量。

如果这是某种标准,则应寻找一个可解析此类字符串或自己编写的库。

显然可以更好地命名变量