[Python] Converting strings in an array to dates

Discussão em 'Python' iniciado por Stack, Agosto 21, 2025.

  1. Stack

    Stack Membro Participativo

    I have read an array of strings from a TXT file and saved it as an array (of over thousand values) using such a line:

    dates = np.genfromtxt('filename.txt', delimiter=";", usecols=(0), dtype=None)


    Next, I would like to convert strings into dates. I tried to use the line:

    dates2 = dt.datetime.strptime([dates], '"%Y-%m-%d"').date()


    however, I got an error:


    TypeError: must be string, not list
    ​

    I figured out that such a code works fine:

    data1='"2010-02-28"'
    data1_1 = dt.datetime.strptime(data1, '"%Y-%m-%d"').date()


    How should I deal with the described problem?

    Continue reading...

Compartilhe esta Página