Python variables cannot be preceded by numbers _python

Source: Internet
Author: User
Tags in python

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.