Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Python - Formattazione stringa
Forum - Python - Formattazione stringa

Avatar
Dante.cpp (Normal User)
Pro


Messaggi: 65
Iscritto: 23/11/2011

Segnala al moderatore
Postato alle 19:37
Mercoledì, 19/12/2012
Salve, vorrei scrivere un piccolo script che, dati in input una serie di stringhe nella forma "4f 6a 23 d9 10" di lunghezza arbitraria, restituisca un'unica stringa in formato "\0x1a\0xa1\0x92\...", ottenuta anteponendo "\0x" ad'ogni coppia di cifre separata da spazio, per ogni stringa immessa .
Io mi stavo approcciando cosi:
Codice sorgente - presumibilmente Python

  1. stringa = ""
  2. byte = ""
  3.  
  4. while byte != "fine" :
  5.         byte = raw_input("byte:")
  6.        
  7.         for i in range( len(byte) ):
  8.                 stringa += "\\0x"+byte[i]+byte[i+1]
  9.  
  10. print stringa


PM Quote
Avatar
Poggi Marco (Member)
Guru


Messaggi: 969
Iscritto: 05/01/2010

Segnala al moderatore
Postato alle 21:54
Mercoledì, 19/12/2012
Per risolvere il problema, ti sarà utile utilizzare la finzione split().

Dalla documentazione ufficiale:
Codice sorgente - presumibilmente Python

  1. str.split([sep[, maxsplit]])
  2. Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).
  3.  
  4. If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2']). The sep argument may consist of multiple characters (for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']). Splitting an empty string with a specified separator returns [''].
  5.  
  6. If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].
  7.  
  8. For example, ' 1  2   3  '.split() returns ['1', '2', '3'], and '  1  2   3  '.split(None, 1) returns ['1', '2   3  ']


Ultima modifica effettuata da Poggi Marco il 19/12/2012 alle 21:59
PM Quote
Avatar
Dante.cpp (Normal User)
Pro


Messaggi: 65
Iscritto: 23/11/2011

Segnala al moderatore
Postato alle 0:09
Giovedì, 20/12/2012
Mi sto innamorando del python, in c sarebbero statti necessari 10 file di programma!!!:D
Codice sorgente - presumibilmente Python

  1. import string
  2.  
  3. stringa = ""
  4. byte = ""
  5.  
  6. while byte != "fine":
  7.         byte = raw_input("byte:")
  8.        
  9.         lista = string.split(byte)
  10.        
  11.         for i in range( len(lista) ):
  12.                 stringa += "\\x"+lista[i]
  13.  
  14. stringa = stringa[0:len(stringa)-5]
  15.  
  16. print stringa



Grazie.

Ultima modifica effettuata da Dante.cpp il 20/12/2012 alle 0:11
PM Quote
Avatar
Poggi Marco (Member)
Guru


Messaggi: 969
Iscritto: 05/01/2010

Segnala al moderatore
Postato alle 11:20
Giovedì, 20/12/2012
In realtà la variabile lista non serve:
Codice sorgente - presumibilmente Python

  1. byte = ""
  2.  
  3. while byte != "fine":
  4.         byte = raw_input("byte:")
  5.        
  6.         stringa=""
  7.        
  8.         for i in string.split(byte):
  9.                 stringa += "\\x"+i
  10.  
  11. stringa = stringa[0:len(stringa)-5]
  12.  
  13. print stringa


PM Quote
Avatar
Dante.cpp (Normal User)
Pro


Messaggi: 65
Iscritto: 23/11/2011

Segnala al moderatore
Postato alle 11:51
Giovedì, 20/12/2012
Fantastico, inoltre:
Codice sorgente - presumibilmente Python

  1. import string
  2.  
  3. stringa = ""
  4. byte = ""
  5.  
  6. while byte != "fine":
  7.         byte = raw_input("byte:")
  8.        
  9.         for i in string.split(byte):
  10.                 stringa += "\\x"+i
  11.  
  12. print stringa[0:len(stringa)-5]


PM Quote