python - xonsh 中遍历文件行的最佳方法是什么?

标签 python bash xonsh

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/

相关文章:

python - 在python中如何在其静态方法中获取类的名称

bash - 如何在bash中捕获和修改正则表达式识别的数字

bash - 为进程设置时间限制并将标准错误写入文本文件

python - 使用 log4j conversionpattern 进行 Pyspark 日志记录不起作用

python - R 的 browser() 在 Python 中等效

python - 在 QGraphicsScene 中连续移动 QGraphicsItem 并检查 Collision

linux - 获取 grep 搜索的结果并使用它来搜索另一个文件。 Linux 脚本

zsh - 将 eval $(pyenv init -) 从 zsh 移动到 xonsh

python-3.x - 使用 xonsh 进行列表理解

python - 如何在 xonsh 中运行 Python 文件?