【问题标题】:Can someone explain me the 5th line of this code?有人可以解释一下这段代码的第 5 行吗?
【发布时间】:2019-06-11 09:28:37
【问题描述】:

我能理解一点,但我想要那条特定行的确切解释。我对语法感到困惑。

否则,我知道代码是如何工作的以及它在做什么,我只是想澄清一下我对语法的概念。

代码:

import docx2txt

def extract_text_from_doc(doc_path):
    temp = docx2txt.process("resumes/Chinmaya_Kaundanya_Resume.docx")
    text = [line.replace('\t', ' ') for line in temp.split('\n') if line]
    return ' '.join(text)

【问题讨论】:

  • 我知道代码是如何工作的以及它在做什么。那么你认为第 5 行在做什么?
  • 您是指以text = 开头的行(第5 行)还是return(实际代码的第五行)?如果是前者,请参见例如stackoverflow.com/questions/11479392/…;这是一个列表理解

标签: python python-3.x


【解决方案1】:

这是list comprehension 版本:

text = []
for line in temp.split('\n'):
    if line:
        text.append(line.replace('\t', ' '))

它逐行遍历temp,如果该行不为空,则将'\t'(制表符)替换为空格,并将结果放入数组text

【讨论】:

    【解决方案2】:

    它基本上是一个列表理解

    它将遍历每一行,检查行是否为空,然后用空格替换制表符。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      相关资源
      最近更新 更多