Python: how to add three text files to one variable and then split into a list

I am using Python 2.5 and trying to make a variable called total and add three files to it. Then I compress total and convert it to hex and split it into a list of strings with a maximum length of 4096 characters.

I currently have a list of files, and I am concatenating it all this way.

filelist = ['debug.log', 'error.log', 'reclog.log']; total = '' for files in filelist: f = open(files, 'r'); total = total + f.read(); f.close(); compressedtotal = zlib.compress(total); hextotal = compressedtotal.encode('hex'); 

Here I feel that I am doing it effectively. But then I try to split hextotal into a list of strings called msglist. But the maximum length of each line in msglist should be no more than 4096 characters.

 if len(hextotal)%4096 >0 : checker = 1; else: checker = 0; nmsgs = int(math.ceil(len(hextotal)/4096)); nn = str(nmsgs); msglist = []; for msgs in range(1,nmsgs+1): if msgs == nmsgs and checker == 1: msglist.append(hextotal[4096*(msgs-1):]); else: msglist.append(hextotal[4096*(msgs-1):4096*(msgs)]); 

I need a simpler way because it is not "pythonic", but im completely new to python. Extremely much.

+4
source share
4 answers

My personal favorite is understanding the list:

 msglist = [ hextotal[i:i+4096] for i in range(0,len(hextotal),4096) ] 
+3
source
 msglist = [] for start in range(0, len(hextotal), 4096): msglist.append(hextotal[start:start+4096]) 

You can cut outside of something; it will automatically just give you only to the end of the line (for example, slicing "abcd"[2:6] will give you cd ).

As CSkau noted, this can also be done in the form of a list comprehension:

 msglist = [hextotal[i:i+4096] for i in range(0, len(hextotal), 4096)] 
+2
source

You can define a generator that returns pieces:

 def chunkify(l, n): for i in xrange(0, len(l), n): yield l[i:i+n] 

Then you can either iterate over the pieces

 msglist = [] for chunk in chunkify(hextotal, 4096): msglist.append(chunk) 

or create a final list this way

 msglist = list(chunkify(hextotal, 4096)) 
+1
source

A few thoughts:

  • There is no need for semicolons when completing the end of a line.
  • No need to create a generator or calculate rounded piece numbers
  • Avoid using + = to create long lines

Instead, enable work with labels and lists:

 filelist = ['debug.log', 'error.log', 'reclog.log']; total = [open(filename, 'r').read() for filename in filelist] compressedtotal = zlib.compress(''.join(total)) hextotal = compressedtotal.encode('hex') msglist = [hextotal[i:i+4096] for i in range(0, len(hextotal), 4096)] 
+1
source

All Articles