Function: String.Join ()
Python has join () and Os.path.join () two functions, which work as follows:
Join (): An array of connection strings. Generates a new string from a string, tuple, or element in the list, with the specified character (delimiter) connection
Os.path.join (): Combine multiple paths to return
First, function description
1. Join () function
Syntax: ' Sep '. Join (SEQ)
Parameter description
Sep: Delimiter. Can be empty
SEQ: sequence of elements to concatenate, strings, tuples, dictionaries
The syntax above is to combine all the SEQ elements into a new string with Sep as a delimiter
Return value: Returns a string that is generated after each element is concatenated with the delimiter Sep
2. Os.path.join () function
Syntax: Os.path.join (path1[,path2[,......])
Return value: Combine multiple paths to return
Note: Parameters before the first absolute path are ignored
Second, examples
1. Manipulate the sequence (with '. ') As a delimiter)
seq = [' Hello ', ' good ', ' boy ', ' Doiido ']
Print ('. '). Join (SEQ))
Hello.good.boy.doiido
2. Operations on tuples (with ': ' as delimiters)
Seq = (' hello ', ' good ', ' boy ', ' Doiido ')
Print (': '. Join (SEQ))
Hello:good:boy:doiido
3. Working with Dictionaries
Seq = {' Hello ': 1, ' good ': 2, ' Boy ': 3, ' Doiido ': 4}
Print (': '. Join (SEQ))
Doiido:boy:hello:good
4. Merging catalogs
Import OS
Print (Os.path.join ('/hello/', ' good/boy/', ' Doiido '))
/hello/good/boy/doiido
Three. JSON dictionary to SQL statement
#表名polls_questions
table_name = "Polls_questions"
#需要插入的Json数据
data={' id ': 1, ' question_text ': ' You buy Pro6? ', ' pub_date ': ' 2016-07-23 09:58:56.000000 '}
#对每一个值加单引号
For key in data:
Data[key] = "'" + str (Data[key]) + "'"
The #利用join () function merges IDs, Question_text, pub_date together (id,question_text,pub_date)
Key = ', '. Join (Data.keys ())
#利用join () function merges the values together (' 1 ', ' You buy Pro6? ', ' 2016-07-23 09:58:56.000000 ')
Value = ', '. Join (Data.values ())
#INSERT into Polls_questions (id,pub_date,question_text) VALUES (' 1 ', ' 2016-07-23 09:58:56.000000 ', ' buy Pro6? ')
Real_sql = "INSERT into" + table_name + "(" + key + ") VALUES (" + Value + ")"
The use of the Join () function in Python