xonsh
中最好的方法是什么? shell 循环遍历文本文件的行?
(A) 目前我正在使用
for l in !(cat file.txt):
line = l.strip()
# Do something with line...
(B)当然还有
with open(p'file.txt') as f:
for l in f:
line = l.strip()
# Do something with line...
我使用 (A) 因为它更短,但是还有什么更简洁的吗? 并且最好折叠 l.strip()
进入循环?
注意:我的主要兴趣是简洁(在小字符数的意义上)- 如果有帮助,可能会使用 xonsh 的特殊语法功能。
最佳答案
您可以将 str.strip()
折叠到带有 map()
的循环中:
(一):
for l in map(str.strip, !(cat file.txt)):
# Do something with line...
(乙):
with open('file.txt') as f:
for l in map(str.strip, f):
# Do something with l..
关于python - xonsh 中遍历文件行的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53254629/