can only concatenate list (not "str") to list

0

Hello, I'm new to python and I'm probably programming with a problem that should be very conceptual. I made this code to read a numeric sequence in a txt and rename it with the string found.

name_files3 = os.listdir(path_txt)

for TXT in name_files3:
    with open(path_txt + '\' + TXT, "r") as content:
        search = re.search(r'(?:\d(?:[\s,.\-\xAD_]|(?:\r)|(?:\n))*){17}', content.read())
    if search is not None:
        name3 = search.group(0)
        # name3 = name3.replace("\n", "")
        # name3 = name3.replace("-", "")
        # name3 = name3.replace("\", "")
        # name3 = name3.replace(".", "")
        # name3 = name3.replace(".", "")
        # name3 = name3.replace("/", "")
        # name3 = name3.replace(" ", "")
        fp = os.path.join("17_digitos", [letter for letter in name3 if letter.isdigit()] + "_%d.txt")
        postfix = 0
        while os.path.exists(fp % postfix):
            postfix += 1
        os.rename(
            os.path.join(path_txt, TXT),
            fp % postfix
        )

At first I was using the replace function to remove all characters that appeared between numbers before renaming. It turns out that there are characters in unicode that "look" hyphen but are not, so at the time of renaming some cases it was not working. So I used [letter for letter in name3 if letter.isdigit()] to just get the numbers that appear. It returns this error:

    fp = os.path.join("17_digitos", [letter for letter in name3 if letter.isdigit()] + "_%d.txt")
TypeError: can only concatenate list (not "str") to list

I tried to change name3 to string and did not solve much

    
asked by anonymous 30.11.2017 / 13:45

1 answer

1

Uses regex in own name3 like this:

re.sub(r"\D","",name3) 

This will remove everything that is not a string number.

    
01.12.2017 / 23:58