关于Python的join()方法问题
代码如下:
import os
import time
source = [r'e:\python',r'e:\temp']
#print(source)
print('源:')
for spath in source:
print(spath)
target_dir = r'e:\bak'
print('\n目的地:')
print(target_dir)
target = time.strftime('%Y%m%d%H%M%S') + '.rar'
zip_command = 'rar a %s\%s %s' % (target_dir,target,' '.join(source))
if os.system(zip_command) == 0:
print('Successful backup to',target)
else:
print('Backup FAILED')
问题:
请教一下,' '.join(source)的作用是将列表source中的项目转换为字符串,那么为什么要在这个方法前加上' '.?这是什么用途?为什么不能直接使用join(source)?我是初学者,麻烦各位不吝赐教,谢谢!
因为他是str的一个方法,不是通用函数。即使有个通用函数也应该像join(source, sep)多个分隔符参数才会相当...
对喔, str的一个方法
>>> s=''
>>> dir(s)
.......'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', ...
你想直接用join也可以的啊,没问题:
import string
string.join(["aaaa", "bbb"])
或者:
from string
相关问答:
像 C 的 scanf() 那样
比如读入 1 2 3 a b c
每次读入一个
下面这个应该是你想要的吧:
Python code:
>>> k = raw_input()
0 0123 ds dsl sd
>>> k
'0 0123 ds dsl sd'
>>> ......
s='aaa111aaa,bbb222,333ccc,444ddd444,555eee666,fff777ggg'
用正则表达式取出 前后字母相同的数据 结果如下:
111 ddd
谢谢~
Python code:
import re
s='aaa111aaa,bbb222,333ccc,444ddd444,555eee666,ff ......
由于是初学,在些请教大家。
Python code:
from urllib import request
import struct
class csdn:
def __init__(self):
print ('Hello,this is a init')
def getContent(self,url) ......
python 核心编程(第二版) 中 ,11.8.4 闭包有个例子
def counter(start_at=0):
count = [start_at]
def incr():
count[0] += 1
return count[0]
return incr
难道其中count[] ......