嗨,我在python脚本上遇到了问题,我不能将它返回的两个字符串数组连在一起-
SyntaxError:不能分配给函数调用
这是我的剧本:
import os, sys, MySQLdb
# connecting to a database
db = MySQLdb.connect(host="localhost", user="MyUser", passwd="MyPassword",db="MyDB")
cursor = db.cursor()
cursor.execute("SELECT * FROM MyTable")
db.commit()
# get the number of rows in the resultset
numrows = int(cursor.rowcount)
# Opening a file to write in it a script
fname = os.path.normpath("C:\Users\Me\Desktop\MyScript.cmd")
f = open(fname, "w")
# get and display one row at a time.
for x in range(0,numrows):
row = cursor.fetchone()
print row[1], "-->", row[2]下面是我想做的;使row1字符串不超过16个字符,如果是,将它放在row2的开头,并将row1切片为16个字符
这是我第一次尝试的方法-
#if len(str(row[1])>=16:
# str(row[2])=str(row[1])+" "+str(row[2])
# str(row[1])=str(row[1][0:14])但是由于它现在不起作用,所以我尝试不测试字符串长度,直接将row1放在row2中,而不管row1有多长
str(row[2])=str(row[1])+" "+str(row[2])
str(row[1])=str(row[1])[:16]
f.write("vtaddapp /Nom=test/%s /Comm=\"%s\"\n" % (str(row[1]), str(row[2])))
f.close()我使用python2.7并使用Windows操作系统
发布于 2015-06-16 09:09:13
只需将第一次尝试更改为:
if len(str(row[1])) >= 16:
row[2] = str(row[1])+" "+str(row[2])
row[1] = str(row[1][0:16])我的答案是根据“系统发生”的评论自由改编的。
更新:上面的答案行不通,因为行是一个元组,因此它是不可变的。您需要将1和2的值赋值给其他变量,并在进一步的处理中使用这些变量。
if len(str(row[1])) >= 16:
temp2 = str(row[1])+" "+str(row[2])
temp1 = str(row[1][0:16])
else:
temp2 = str(row[2])
temp1 = str(row[1])一个简单的方法是:
temp2, temp1 = str(row[1])+" "+str(row[2]), str(row[1][0:16]) if len(str(row[1])) >= 16 else str(row[2]), str(row[2])https://stackoverflow.com/questions/30862422
复制相似问题