python变量不能以数字打头详解

作者:jingxian 时间:2022-06-30 15:33:48 

在编写python函数时,无意中发现一个问题:python中的变量不能以数字打头,以下函数中定义了一个变量3_num_varchar,执行时报错。

函数如下:


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 is not varchar')
   return feild_split

报错信息如下:


>>> 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-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
 execfile(filename, namespace)
File "D:\Python33\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 88, in execfile
 exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "E:/procedure/python/projects/others/table_test.py", line 20
 3_num_varchar = int(num_varchar)*3
       ^
SyntaxError: invalid syntax

将变量3_num_varchar改为num_varchar_3,运行成功,程序改为如下:

import os
import sys
str1='aaa varchar(10)'

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)
   num_varchar_3 = num_varchar*3
   feild_split[1] = feild_split[1].replace(str(num_varchar),str(num_varchar_3))
   return feild_split
 else:
   print ('The feild type is not varchar')
   return feild_split

print (database_feild_varchar_trans(str1))

运行结果:


>>> runfile('E:/procedure/python/projects/others/table_test.py', wdir='E:/procedure/python/projects/others')
The feild type is not varchar
['aaa', 'varchar(10)']

标签:python,数字,变量
0
投稿

猜你喜欢

  • 处理Python中的URLError异常的方法

    2021-06-04 03:33:15
  • python发腾讯微博代码分享

    2022-05-27 04:45:00
  • asp动态调用不同include文件方法

    2007-09-26 14:22:00
  • 浅谈JavaScript的自动垃圾收集机制

    2023-08-13 15:18:57
  • python去除字符串中的换行符

    2021-07-11 12:35:23
  • IntelliJ IDEA卡死,如何优化内存

    2023-07-04 12:10:27
  • python 协程并发数控制

    2023-09-20 01:07:10
  • python pandas 数据排序的几种常用方法

    2021-10-02 06:25:01
  • python实现基于朴素贝叶斯的垃圾分类算法

    2021-01-18 16:46:06
  • Python中functools模块的常用函数解析

    2022-08-12 08:10:50
  • 原生js实现Flappy Bird小游戏

    2024-04-29 13:37:03
  • Python执行ping操作的简单方法

    2022-09-22 12:09:22
  • 详解Vue开发网站seo优化方法

    2024-04-10 13:47:53
  • 巧用weui.topTips验证数据的实例

    2023-08-12 03:00:51
  • PyCharm在win10的64位系统安装实例

    2022-10-14 20:24:46
  • Python接单的过程记录分享

    2022-05-24 13:33:23
  • 学会python自动收发邮件 代替你问候女友

    2023-07-08 17:01:49
  • MySQL-MMM安装指南(Multi-Master Replication Manager for MySQL)

    2024-01-19 04:46:03
  • Python关于OS文件目录处理的实例分享

    2022-12-29 08:52:07
  • python文件目录操作之os模块

    2023-01-10 14:22:59
  • asp之家 网络编程 m.aspxhome.com