Manipulating strings in Python

Asked

Viewed 572 times

1

I have a list with n strings. Example:

lista = ['0004434-48.2010', 'UNIÃO, '(30 dias úteis) 03/07/2017', '13/07/2017', '0008767-77.2013', 'UNIÃO, '(10 dias úteis) 03/07/2017', '13/07/2017']

My program scrolls through this list, but all items are presented as strings. I need:

  1. Detect among them what has DATA format;
  2. Treat this string (convert to date format);
  3. Detect between them what has NUMBERS;
  4. Treat this string (convert to number format).

Any idea?

  • What would be the number format? None there apparently is a number.

  • 1

    As it is string I believe that you can do gande part of what you want with regex and split

  • Here 'UNIÃO, '(10 dias úteis) 03/07/2017' looks to me like there’s a ' missing. Better confirm

  • I appreciate all your help! My problem is that the date I’m going to use is along with this phrase "union, (10 working days)", so I couldn’t use exactly the suggested suggestion, but I decided to deal with "2017 in string".

2 answers

4


from datetime import datetime
lista = ['0004434-48.2010',
 'UNIÃO',
 '(30 dias úteis) 03/07/2017',
 '13/07/2017',
 '0008767-77.2013',
 '2017',
 '(10 dias úteis) 03/07/2017',
 '13/07/2017']


for s in lista:
  try:
    print('É data: ', datetime.strptime(s, '%d/%m/%Y'))
  except:
    try:
      print ('É numero, convertido para inteiro ',int(s))
    except:
      print('É string:  ', s )

Output:

É string:   0004434-48.2010
É string:   UNIÃO
É string:   (30 dias úteis) 03/07/2017
É data:  2017-07-13 00:00:00
É string:   0008767-77.2013
É numero, convertido para inteiro  2017
É string:   (10 dias úteis) 03/07/2017
É data:  2017-07-13 00:00:00

Alternative:

## Versão 2
print ('#########################')

for s1 in lista:
  for s in s1.split():
    try:
      print('É data: ', datetime.strptime(s, '%d/%m/%Y'))
    except:
      try:
        print ('É numero, convertido para inteiro ',int(s))
      except:
        print('É string:  ', s )

Output:

#########################
É string:   0004434-48.2010
É string:   UNIÃO
É string:   (30
É string:   dias
É string:   úteis)
É data:  2017-07-03 00:00:00
É data:  2017-07-13 00:00:00
É string:   0008767-77.2013
É numero, convertido para inteiro  2017
É string:   (10
É string:   dias
É string:   úteis)
É data:  2017-07-03 00:00:00
É data:  2017-07-13 00:00:00

Run the code in repl.it.

0

Remembering that single- or double-quote characters form strings, so if you wrote each item between single-quote quotes, each item will be a different string. If you want to use another format (date, for example), use the date format and so on

  • 1

    But a string in date format remains a string. What exactly did you mean by that? In fact, it has as its answer [Edit] and to present an example of code of this solution that indicated?

  • Ahhhh yes, I thought you just wanted to transform the data. You want to identify, AMONG your strings, what is date and number, right? if so, just create an if. Hence if the string has the date format, that would be dd/mm/yyyy, just vc create the condition :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.