for v in enumerate (["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"])
folder = "/Users/Name/Desktop/Datas/" + v
csvFiles = glob.glob (os.path.join (folder, "*.csv"))
df = (pd.read_csv(i) for i in csvFiles)
df = pd.concat (df, ignore_index=True)
我在子文件夹A,B,C,D,E,F,G,H,I,J,K上进行迭代。
这段代码很好用,但我怎么能避免写所有字符串[“A”,“B”,“C”,“D”,“E”,“F”,“G”,“H”, for 句子中的“我”,“J”,“K”]?
答案 0 :(得分:1)
如果你根据号码开始工作,你可以这样做:
import string
letters = [string.ascii_uppercase[i] for i in range(11)]
答案 1 :(得分:0)
如果您使用连续字母,可以使用:
for v in range(ord('A'), ord('K')+1):
print(chr(v))
答案 2 :(得分:0)
字符串也是可迭代的:)
Python 2.7.14+ (default, Dec 5 2017, 15:17:02)
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> for x in enumerate('ABC'):
... print x
...
(0, 'A')
(1, 'B')
(2, 'C')
你可以比较字符串:
>>> 'A' < 'V'
True
>>> 'E' < 'V'
True
>>> 'E' < 'D'
False
>>> 'A' < 'C' < 'K'
True