When writing a Python function, I stumbled upon a problem: A variable in Python cannot begin with a number, and the following function defines a variable 3_num_varchar, which executes the times incorrectly.
The functions are as follows:
def Database_feild_varchar_trans (in_feild):
' Transfer the
feild if varchar then 3times lang Else no transfer
'
feild_split = In_feild.split (')
Is_varchar = feild_split[1].find (' varchar ')
if Is_varchar >= 0 :
Num_varchar = feild_split[1].replace (' varchar ', '). Replace (' (', ') '. Replace (') ', ')
print (Num_varchar)
3_num_varchar = num_varchar*3
feild_split[1] = feild_split[1].replace (str (NUM_VARCHAR), str (3_num_ varchar)) return
feild_split
else:
print (' The feild type isn't varchar ') return
Feild_split
The error message is as follows:
>>> runfile (' e:/procedure/python/projects/others/table_test.py ', wdir= ' e:/procedure/python/projects/ Others ') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "D:\Python33\lib\site-p" ackages\spyderlib\widgets\externalshell\sitecustomize.py ", line 699, in Runfile execfile (filename, namespace) File" D: \python33\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py ", line, in execfile exec (compile ( Open (filename, ' RB '). Read (), filename, ' exec '), namespace File "e:/procedure/python/projects/others/table_test.py", Line 3_num_varchar = Int (num_varchar) *3 ^ syntaxerror:invalid syntax change variable 3_num_varchar to num_varchar_3, run successfully, process To be replaced by the following: Import OS import sys str1= ' aaa varchar ' def database_feild_varchar_trans (in_feild): ' Transfer the Feil d if varchar then 3times lang else no transfer ' feild_split = In_feild.split (') Is_varchar = Feild_split[1].fin D (' VARCHAR ') if Is_varchar >= 0:num_vArchar = Feild_split[1].replace (' VARCHAR ', '). Replace (' (', ', '). Replace (') ', ') print (Num_varchar) num_varchar_3 = Num_varchar*3 feild_split[1] = feild_split[1].replace (str (NUM_VARCHAR), str (num_varchar_3)) return Feild_split E
Lse:print (' The feild type isn't varchar ') return feild_split print (Database_feild_varchar_trans (STR1))
Run Result:
>>> runfile (' e:/procedure/python/projects/others/table_test.py ', wdir= ' e:/procedure/python/projects/ Others ')
the feild type is not varchar
[' AAA ', ' varchar (10) ']
The above Python variable can not be a number of the beginning of the explanation is small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.