【问题标题】:parsing text file for a line, in python在python中解析一行的文本文件
【发布时间】:2011-08-04 11:21:43
【问题描述】:

系统生成一个文本文件。它包含 100 多行。我喜欢在文件中找到一行。

some text **
Actions Pending are: Action-1, Action-2,....Action-3 (this is another new line)
some text**

需要获取待处理的动作到数组。

我用过

for index in text:
    rc.logMessage(str(index))

它一次打印每个字符而不是一行。

帮助我如何解析此文件以将操作放入数组中。

提前致谢

【问题讨论】:

    标签: python file parsing text


    【解决方案1】:

    类似:

    d = """some text **
    Actions Pending are: Action-1, Action-2, Action-3
    some text**
    """
    res = []
    for line in re.findall('Actions Pending are: (.+)', d):
        res.extend([action.strip() for action in line.split(',')])
    ['Action-1', 'Action-2', 'Action-3']
    

    【讨论】:

      【解决方案2】:

      你可以试试这样的:

      pendingActions = []
      textToSearch = 'Actions Pending are:'
      for line in open(filename, 'r'):
          line = line.strip()
          if line and line.startswith(textToSearch):
              pendingActions.extend([x.strip() for x in line[len(textToSearch):].split(',') if x.strip()])
      

      【讨论】:

        【解决方案3】:

        您需要遍历文件,而不是从文件中读取的字符串。

        with open(filename) as text:
            for line in text:
                 rc.logMessage(some_function_of_the_line(line))
        

        遍历文件会给你行;遍历字符串会给你字符/字节。

        【讨论】:

          【解决方案4】:

          你想要str.splitlines()http://docs.python.org/library/stdtypes.html#str.splitlines

          for index in text:
              rc.logMessage(str(index))
          

          变成:

          for index in text.splitlines():
              rc.logMessage(str(index))
          

          【讨论】:

            【解决方案5】:

            试试这样的

            with file("your_file") as logfile:
               result = [line for line in logfile if line.startswith("Actions pending")]
            

            这样你就会得到所有的动作线。

            【讨论】:

              【解决方案6】:
                  search_string = 'Actions Pending are: '
                  for line in open('yourfile.txt', 'r').readlines():
                    if line.startswith(search_string):
                      actions = line[len(search_string):].split(',')
                      break
                  print actions
              

              Artsiom 更快:parsing text file for a line, in python,也许我的版本更具可读性。

              【讨论】:

                【解决方案7】:

                这是一个单行(为了好玩):

                s = """some text **
                Actions Pending are: Action-1, Action-2, Action-3
                Actions Pending are: Action-4, Action-5, Action-6
                some text**"""
                
                [a for ln in s.splitlines() if ln.startswith("Actions Pending") for a in ln[len("Actions Pending are: "):].split(', ')]
                ------
                ['Action-1', 'Action-2', 'Action-3', 'Action-4', 'Action-5', 'Action-6']
                

                要使用文件而不是字符串,请将 s.splitlines() 替换为 f.readlines()。请注意,我不会在实践中使用此代码;这只是为了好玩。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2020-05-19
                  • 1970-01-01
                  • 2022-01-17
                  • 2017-01-23
                  • 1970-01-01
                  • 1970-01-01
                  • 2018-03-08
                  • 1970-01-01
                  相关资源
                  最近更新 更多