Coder123
Coder123

Reputation: 344

How to merge for loop results in one list

I am trying to merge all the values into one list when I run my for loop. However I keep getting to separate brackets in one list.

For example, when I run this code:

import glob
import re
#import PyPDF2

folder_path='/Users/my_path/cb_files'
file_pattern = "/*"
folder_contents = glob.glob(folder_path + file_pattern, recursive=True)

#IP Bank
import re 

ip = re.compile(r"((?:^|\b)(?:h[tTxX]ps?://)?(?:\d{1,3}\[?\.\]?){3}\d{1,3}(?:\b|$))")
hash_ = re.compile(r"((?:^|\b)[a-fA-F0-9]{32,64}(?:\b|$))")
domain = re.compile(r"((?:^|\b)(?:h[xXtT]{2}ps?:|meows?:)?//(?:[a-zA-Z0-9\u00A0-\uD7FF\uF900-\uFDFC\uFDF0-\uFFEF_.\[\]-]+)(?:\[?\.\]?[a-z]+)+(?::\d+)?(?:[/?][^\s]*)?)")

ip_list=[]

for file in folder_contents:
    if re.search(r".*(?=pdf$)",file):
        #this is pdf
        pdfFileObj = open('pdf.pdf', 'rb')
        pdfReader = PyPDF2.PdfFileReader(pdfFileObj) 
        pageObj = pdfReader.getPage(0) 
        read_file=pageObj.extractText()      
    elif '.' not in file:
        continue
    else:
         read_file = open(file, 'rt', encoding="latin-1").read()

    if  ip.findall(read_file) or hash_.findall(read_file) or domain.findall(read_file):
        ips =ip.findall(read_file)
        hashs= hash_.findall(read_file)
        domains=domain.findall(read_file)
           # print("IPS",', '.join(ips))
        ip_list.append(ips)

print(ip_list)

Here is my output:

[['000.000.0.1', '111.111.1.1'], ['222.222.2.2','333.333.3.3']]

So it looks like for each file it loops over, it is putting it in its own list.

I want the output to look like this:

['000.000.0.1', '111.111.1.1','222.222.2.2','333.333.3.3']

Any changes in my code that will produce these results?

Upvotes: 0

Views: 405

Answers (1)

Dhaval Taunk
Dhaval Taunk

Reputation: 1672

Try changing:-

ip_list.append(ips)

to

ip_list.extend(ips)

Upvotes: 1

Related Questions