Python 2.6引入了该str.format()方法,其语法与现有%运算符略有不同.哪种情况更好,哪种情况更好?
以下使用每种方法并具有相同的结果,那么有什么区别?
#!/usr/bin/python
sub1 = "python string!"
sub2 = "an arg"
a = "i am a %s" % sub1
b = "i am a {0}".format(sub1)
c = "with %(kwarg)s!" % {'kwarg':sub2}
d = "with {kwarg}!".format(kwarg=sub2)
print a # "i am a python string!"
print b # "i am a python string!"
print c # "with an arg!"
print d # "with an arg!"
Run Code Online (Sandbox Code Playgroud)此外,何时在Python中发生字符串格式化?例如,如果我的日志记录级别设置为HIGH,我仍然会执行以下%操作吗?如果是这样,有没有办法避免这种情况?
log.debug("some debug info: %s" % some_info)
Run Code Online (Sandbox Code Playgroud)我想int加入一个string.这就是我现在正在做的事情:
num = 40
plot.savefig('hanning40.pdf') #problem line
Run Code Online (Sandbox Code Playgroud)
我必须运行几个不同数字的程序,而不是两个40.所以我想做一个循环但插入这样的变量不起作用:
plot.savefig('hanning', num, '.pdf')
Run Code Online (Sandbox Code Playgroud)
如何将变量插入Python字符串?
所以,我有这个问题.我得到了元组(1,2,3),我应该用字符串格式打印.例如.
tup = (1,2,3)
print "this is a tuple %something" % (tup)
Run Code Online (Sandbox Code Playgroud)
这应该用括号打印元组表示,比如
这是一个元组(1,2,3)
但我得到了TypeError: not all arguments converted during string formatting.
我能在世界上做到这一点吗?有点失去了这里,如果你们可以指出我正确的方向:)
这就是我在做的事情
>>> import logging
>>> logging.getLogger().setLevel(logging.INFO)
>>> from datetime import date
>>> date = date.today()
>>> logging.info('date={}', date)
Traceback (most recent call last):
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 846, in emit
msg = self.format(record)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 723, in format
return fmt.format(record)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 464, in format
record.message = record.getMessage()
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 328, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting
Logged from file <stdin>, line 1
>>>
Run Code Online (Sandbox Code Playgroud)
我的python版本是
$ python --version
Python …Run Code Online (Sandbox Code Playgroud) Python版本-2.7.6
熊猫版-0.17.1
MySQLdb版本-1.2.5
DataFrame.to_sql() 扔 pandas.io.sql.DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': not all arguments converted during string formatting
Python代码段
con = MySQLdb.connect('localhost', 'root', '', 'product_feed')
cur = con.cursor()
cur.execute("SELECT VERSION()")
connection_result = cur.fetchall()
print connection_result[0][0] #It prints 5.5.44-0ubuntu0.14.04.1
table_column = ['A', 'B', 'C']
created_data = numpy.array([numpy.arange(10)]*3).T
df = pandas.DataFrame(data=created_data ,columns=table_column)
df.to_sql('test_table', con)
Run Code Online (Sandbox Code Playgroud)
该错误来自df.to_sql('test_table', con)行的执行。
错误详情
File "/home/yogi/yogi/mlPython/product_feed/etl_pf/process_data.py", line 298, in render_df
df.to_sql('test_table', con)
File "/home/yogi/yogi/mlenv/local/lib/python2.7/site-packages/pandas/core/generic.py", line 1003, in to_sql
dtype=dtype)
File …Run Code Online (Sandbox Code Playgroud) 假设我们有一些像这样的代码:
placehold = "6"
string1 = "this is string one and %s" % placehold
print string1
placehold = "7"
print string1
Run Code Online (Sandbox Code Playgroud)
运行时,两个 print 语句都返回,就好像 placehold 始终为 6。但是,就在第二条语句运行之前,placehold 更改为 7,那么为什么它没有动态反映在字符串中呢?
另外,您能建议一种使字符串返回 7 的方法吗?
谢谢
我正在尝试将变量“log_location”的内容添加到当前记录器。
log_location = log_folder_location + os.path.sep + log_file_name
logger.debug("log location", str(log_location))
print "log_location: ",log_location
Run Code Online (Sandbox Code Playgroud)
这会打印到控制台,但在日志记录中出错,
Traceback (most recent call last):
File "/usr/lib64/python2.6/logging/__init__.py", line 784, in emit
msg = self.format(record)
File "/usr/lib64/python2.6/logging/__init__.py", line 662, in format
return fmt.format(record)
File "/usr/lib64/python2.6/logging/__init__.py", line 444, in format
record.message = record.getMessage()
File "/usr/lib64/python2.6/logging/__init__.py", line 314, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting
Run Code Online (Sandbox Code Playgroud)
它打印到,
log_location: /U01/Nova/Logs/DEV-INT/TEST/validation_20170203-164617-5.log
Run Code Online (Sandbox Code Playgroud)
当我尝试正常的 python 提示但在使用 Flask 时面临同样的问题时,不会发生此错误
我已经尝试将Python 标准输出记录到文件...使用活动标准输出(退格/更新) 和类型错误:并非所有参数都在字符串格式化 python 期间转换 …
python ×7
logging ×3
string ×2
formatting ×1
mysql-python ×1
pandas ×1
performance ×1
python-2.7 ×1
sql ×1
variables ×1