1
zk8802 2016-01-25 22:46:49 +08:00 1
|
2
terence4444 OP @zk8802 感谢,连页面 tag 都打好了,太贴心 :)
|
3
ethego 2016-01-25 22:55:35 +08:00 1
建议使用 format 方法,%什么的估计只有写 c 的转 python 才会用。"{0}, {1}{2}".format("hello", "world", 1) >>> "hello, world1"
|
4
terence4444 OP @ethego 感谢,我在文档中看到这个写法和 format 写法等同的,我本来要写动态 SQL 用的……
sql_cause = "replace into tabls(%(fields)s) values(%(values)s) " params = {'fields': 'f1,f2,f3', 'values': 'v1,v2,v3'} sql_cause = sql_cause % params 用 format 应该可以等同为(还没测): "replace into tabls({0}) values({1}) ".format('f1,f2,f3', 'v1,v2,v3') Python 推荐用第二种吗? |
5
ethego 2016-01-25 23:05:05 +08:00 1
@terence4444 推荐第二种,比第一种更加简洁明了,还不用考虑类型的问题
|
6
ethego 2016-01-25 23:09:05 +08:00
@terence4444 写 sql 拼接字符串,小心 sql 注入
|
7
terence4444 OP @ethego 谢谢提醒
这个是我自己跑定时任务抓东西用的, field 和 value 都是固定写死的几个字段的排列组合(抓到某个值就写上字段和值,没有抓到的就跳过不更新) 没有用户输入,应该没有关系吧…… 有用户输入的肯定会 encode 一下的。 |
8
qihboy 2016-01-25 23:19:38 +08:00 1
% 是 python 的表达式
.format 是方法,现在推荐用 format 而已 |
9
vmebeh 2016-01-25 23:23:32 +08:00 1
拼接 sql 语句不安全,用 ? 占位, tuple 传值
https://docs.python.org/2/library/sqlite3.html ```# Never do this -- insecure! symbol = 'RHAT' c.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol) # Do this instead t = ('RHAT',) c.execute('SELECT * FROM stocks WHERE symbol=?', t) print c.fetchone() ``` |
11
necomancer 2016-01-26 01:54:53 +08:00 via Android 2
|
12
RqPS6rhmP3Nyn3Tm 2016-01-26 02:00:45 +08:00 via iPad
写了这么久第一次知道还有 format 写法,果然写 C 写习惯了……
|
13
guoqiao 2016-01-26 03:23:34 +08:00
|
14
leavic 2016-01-26 09:19:26 +08:00
习惯了 format 写法,我都快忘记%方法怎么写了
|
15
nellace 2016-01-26 09:25:38 +08:00
习惯了%,不知道耗能 format
|
16
qihboy 2016-01-26 09:42:20 +08:00
@fy https://docs.python.org/2/library/stdtypes.html#str.format
This method of string formatting is the new standard in Python 3, and should be preferred to the % formatting described in String Formatting Operations in new code. New in version 2.6. |
17
Karblue 2016-01-26 10:07:19 +08:00
%写法写起来更快 。 format 都懒得敲
|
18
huangfs 2016-01-26 10:19:03 +08:00
也是习惯%
|
19
TheCure 2016-01-26 10:37:03 +08:00
这东西叫占位符
要是你知道这叫占位符,搜起来就很快了 |
20
pynix 2016-01-26 10:44:58 +08:00
字符串插值, format 是 Java 那边搞过来的,有点背道而驰。
|
21
fy 2016-01-26 11:25:37 +08:00
@necomancer
@qihboy 虽然 PEP3101 并没有推荐不推荐的内容, str.format 的 3.5 版本文档里也没有这句话,但我在别处发现了一段信息: https://docs.python.org/3.5/library/stdtypes.html#printf-style-string-formatting Note: The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer str.format() interface helps avoid these errors, and also provides a generally more powerful, flexible and extensible approach to formatting text. 他说元组和字典的显示有问题,然而貌似并没有: >>> '%s and %s a' % ((1,2,3), {1:2, 'a':2}) "(1, 2, 3) and {1: 2, 'a': 2} a" 不知道为什么官方文档会有如此明显的倾向性,还没给出合适的例子。我个人觉得 .format 作为一个复杂蛋疼的机制,还是太冗长,不讨喜。 |
22
youngsterxyf 2016-01-26 13:12:40 +08:00
|
23
hitmanx 2016-01-26 13:19:21 +08:00
说找不到也是蛮奇怪的事,我 google "python string percentage sign",前 5 条都是关于这个的,其中第 5 条就是官方doc.
|
24
terence4444 OP @hitmanx 我用 "Python String Operator %" 找的…… 以为是一个 operator ,昨天在家用的 Bing
|
25
hitmanx 2016-01-26 16:31:08 +08:00
@terence4444 不是我较真啊.按照你的关键词搜,无论是 bing 还是 google 前几个结果都是关于这个的啊,莫非是我这里搜索多了排名有优化?
bing 的结果如下..除了第二个可能不对以外,其他的每个都包含了 "% operator", "string formatting"之类的关键字. 7.1. string — Common string operations — Python … https://docs.python.org/2/library/string The string module contains a number ... Python ’ s built-in string classes support ... To output formatted strings use template strings or the % operator described in ... python convert a string to an operator - Stack Overflow stackoverflow.com/.../5117112/python-convert-a-string-to-an-operator RESOLVEDLAST UPDATED: 2/25/20113 POSTSFIRST POST: 2/25/2011 Is it possible to convert a string to an operator in python? I would like to pass a condition to a function Ideally it would look like this: def foo(self, attribute ... performance - Python string formatting: % vs. .format ... stackoverflow.com/questions/5082452 RESOLVEDLAST UPDATED: 12/23/20138 POSTSFIRST POST: 2/22/2011 Python 2.6 introduced the str.format() method with a slightly different syntax from the existing % operator. Which is better and for what situations? The following ... 5. Built-in Types — Python 2.7.11 documentation https://docs.python.org/2/library/stdtypes To output formatted strings use template strings or the % operator described in the String Formatting Operations section. ... String (converts any Python object using str Python Strings - Tutorialspoint www.tutorialspoint.com/python/python_strings.htm String Formatting Operator. One of Python's coolest features is the string format operator %. This operator is unique to strings and makes up for the pack of having ... |
26
se77en 2016-01-26 16:43:20 +08:00
最全的解释 https://pyformat.info
|
27
terence4444 OP @hitmanx 我找的结果好像和你贴的不一样,不过即使一样也没有很多相关信息把引导到 format 上去
7.1. string — Common string operations — …翻译此页 The string module contains a number ... Python ’ s built-in string classes support ... To output formatted strings use template strings or the % operator described in ... python convert a string to an operator - Stack Overflow 最佳答案 Is it possible to convert a string to an operator in python? I would like to pass a condition to a function Ideally it would look like this: def foo(self, attribute ...详情 stackoverflow.com/.../5117112/python-convert-a-string-to-an-operator · 2011-02-25 9.9. operator — Standard operators as functions …翻译此页 This table shows how abstract operations correspond to operator symbols in the Python syntax and the functions in the ... String Formatting: s % obj: mod(s, obj ... 5. Built-in Types — Python 2.7.11 documentation 翻译此页 To output formatted strings use template strings or the % operator described in the String Formatting Operations section. ... String (converts any Python object using str BitwiseOperators - Python Wiki 翻译此页 These are Python's bitwise operators. ... they treat it as if it were a string of bits, ... Python allows operator overloading, ... https://wiki.python.org/moin/BitwiseOperators Operators and String Formatting in Python - …翻译此页 Operators and String Formatting . Terms in This Chapter . Format directives; Hexdump; Key; Keyword; Literal; Modulus; Operator precedence; Boolean value; Class ... www.informit.com/articles/article.aspx?p=28790 在第一个搜索结果中并没有 format 这样的用法,我都是打开然后直接搜索 % 符号找的,知道 % 和 format 是一样的话应该可以找到,但是不知道的话,我觉得并不是那么容易。 |
28
necomancer 2016-01-26 22:05:51 +08:00 via Android
|
29
necomancer 2016-01-26 22:07:22 +08:00 via Android
|
30
necomancer 2016-01-26 22:11:03 +08:00 via Android
我了个脑残,变量名都弄错了, V2EX 好像也不能改
|
31
MinskyNg 2016-01-26 22:29:29 +08:00
新版的 python 教程好像都推荐 format,功能更强大
|
32
kkzxak47 2016-01-26 22:38:42 +08:00
format 更简单,开始用就会喜欢上它
|
33
Arthur2e5 2016-01-26 22:46:16 +08:00 1
@terence4444 第一种 %(foo)s 指定了字段的名称,经常能让东西更可读(特别是翻译软件的时候译者可以直接看到这里是什么东西)——相应地你应该在 format 里面用 "{foo}".format(foo='bar')。
@ethego 考虑类型…… Python 还 Better Explicit than Implicit 呢。主要是 format 功能性重要啦。 |
34
ming2281 2016-01-26 22:55:20 +08:00 via Android
楼主用过 ipython 没
如果没有,推荐试试,大部分问题都会烟消云散 你这种查帮助文档的问题,在 ipython 里面都不值一提 它非常强大,强大到它变成了我现在的默认 linux shell |
35
terence4444 OP @ming2281 这台电脑我还要打游戏……公司里摸鱼写 Python 的电脑也是 Windows 的,所以没有 Linux ……
|
36
terence4444 OP 在附言 1 中总结了这帖的内容,应该算是结帖了,撒花。
|