【问题标题】:Extracting delimited data from .txt file into vector将 .txt 文件中的分隔数据提取到向量中
【发布时间】:2021-09-05 09:23:06
【问题描述】:

我一直在寻找解决此问题的方法,但运气不佳,如果有任何建议或帮助,我将不胜感激。

我在 Jupyter Notebooks 中使用 Python 3。

假设我有一个 words.txt 文件,其中包含如下信息:

(50)  PMC3933789  (view)
(36)  PMC4763238  (view)
(35)  PMC2821926  (view)
(26)  PMC3154047  (view)
(25)  PMC3471816  (view)
(25)  PMC4350884  (view)
(24)  PMC2809798  (view)
(23)  PMC2861733  (view)
(22)  PMC4556980  (view)
(22)  PMC4811477  (view)
(19)  PMC3271181  (view)
(19)  PMC3549280  (view)
(19)  PMC4879671  (view)
(18)  PMC2938390  (view)
(18)  PMC3186417  (view)
(18)  PMC3498278  (view)
(18)  PMC3601842  (view)
(16)  PMC3445503  (view)
(16)  PMC3491835  (view)

在 Python 中,我想读取这个 .txt 文件,提取分隔数字,并将它们分配给一个向量,即:

var = [3933789, 4763238, 2821926...]

以前,我一直在使用 Excel 并手动分隔数字,附加逗号,然后手动复制结果值,但是当参数数量增加时,这很乏味。我希望能够在 Python 中做到这一点。

【问题讨论】:

  • 我认为这应该涵盖您的使用:stackoverflow.com/a/25758542/14518942
  • 有很多逐行读取文本文件的示例。然后在行上使用 split(),并从第 3 个字符中提取。
  • 一种方法是在列表理解中使用正则表达式:import re; with open('mydata.txt', 'r') as file: var = [int(re.search('PMC(\d+)', line).group(1)) for line in file],其中 mydata.txt 是您的文本文件。

标签: python excel extract delimiter


【解决方案1】:

一种在列表推导中使用正则表达式的方法

import re
with open('mydata.txt', 'r') as file:  # mydata.txt is name of data file    
    var = [int(re.search(r'PMC(\d+)', line).group(1)) for line in file]

解释

r'PMC(\d+)'                    - regular expression for capturing digits after PMC
re.search(r'PMC(\d+)', line)   - finds and captures digits in a line
re.search(...).group(1)         - correspond to capture group 1 which are the digits
int(...)                       - converts digits from string to number
for line in file               - iterates through the lines of the file

【讨论】:

    【解决方案2】:

    如果这是一致的模式,您可以使用列表推导来去除固定索引:

    with open('my-file','r') as lines:
        numbers = [int(line[9:16]) for line in lines.readlines() if len(line.strip()) > 0]
    print(numbers)
    

    对于上面的例子,给出:

    [3933789, 4763238, 2821926, 3154047, 3471816, 4350884, 2809798, 2861733, 4556980, 4811477, 3271181, 3549280, 4879671, 2938390, 3186417, 3498278, 3601842, 3445503, 3491835]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-07
      • 1970-01-01
      • 2015-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-28
      • 2019-06-10
      相关资源
      最近更新 更多