【问题标题】:Python: retrieving two separate list values in one file linePython:在一个文件行中检索两个单独的列表值
【发布时间】:2019-05-29 16:26:41
【问题描述】:

我正在编写 python 中的代码,其中文件的一个特定行中有两个单独的值。我想将它们作为单独的部分检索到 matplotlib 的列表中。这是我到目前为止的代码:

with open('data.txt') as data_file:

    def process(line):
        line = line.rstrip(data_file)
        line = line.split('.')[1]
        line = line.split(',')
        return line


    x = list()
    y = list()

    counter = 0

    for line in data_file:
        if (counter == 3) or (counter == 4):
            result = process(line)
            x.append(int(result[0]))
            y.append(int(result[1]))
        counter += 1

print(x)
print(y)

错误是说:

line = line.rstrip(data_file)
TypeError: rstrip arg must be None or str

一个示例文件是:

hi
hi
67, 78
2345, 45677

谁能帮我解决这个错误,或者提供更好的方法来达到同样的结果。任何帮助表示赞赏!

非常感谢!

【问题讨论】:

  • 为什么line = line.rstrip(data_file) 行中有参数data_file
  • @Xteven 我认为line = line.rstrip() 需要引用文件来剥离行
  • rstrip 是属于 Python 中任何字符串对象的方法,它接受字符串的可选参数(其中包含要删除的字符)。更多信息在这里:docs.python.org/3/library/stdtypes.html#str.rstrip
  • @Xteven 非常感谢您的帮助!如果可以,我还有一个问题。当我从代码中删除data_file 时,它说line = line.split('.')[1] 列表索引超出范围。你知道我该如何解决吗?
  • 在您的示例文件中,没有一行包含 . 字符,这意味着 line.split(".") 只返回该行中所有内容的列表,因为没有什么可以分割的。返回列表只有一项(无论该行是什么),因此 Python 抱怨试图访问第二项。

标签: python python-3.x macos matplotlib


【解决方案1】:

这是我能想到的:

import re

regex = r'[\d]{1,3}, [\d]{1,3}'
result = []
with open('sample.txt') as f:
    lines = f.readlines()
    for line in lines:
        match = re.findall(regex, line)
        if match != []:
            splitted = match[0].split(',')
            #the values are mapped to a list containing floating point numbers
            mapped = list(map(float, splitted))
            #and then are appended to a list that will contain all of
            #the lines that have the numbers on it
            result.append(mapped)

    print(result)
    #this is how you could access each line in result
    for list in result:
        print(list)

输出

[[67.0, 78.0], [25.0, 18.0]] #result is a list containing all lines that have the pattern <number>, <number>
[67.0, 78.0] #the first line that matches the pattern
[25.0, 18.0] #the second one

这使用正则表达式来查找最多 3 位数字(但您可以将其更改为您想要的任何数字),匹配模式 &lt;number&gt;, &lt;number&gt;

如果它与模式匹配,它会拆分, 处的两个数字,创建一个包含这两个值的列表并将它们附加到结果列表中

希望对你有帮助。

有任何问题欢迎提出。

编辑

我用这个作为一个示例文件来举例说明:

hi
hi
67, 78
hi again
25, 18

【讨论】:

  • 非常感谢您的帮助!我是 python 的初学者,所以我真的不明白发生了什么——即使你解释了一些!你介意向我解释一下 re 是什么,findall 是,regex 是,最后是 match != 部分。我知道这基本上是整个代码,但我非常感谢您的帮助。再次感谢!
  • “re”是python内置的处理正则表达式的包。当“re”被导入时,它就像一个类,“findall”是它拥有的方法之一。 “findall”,在这种情况下有两个参数,正则表达式“regex”和一个字符串(是示例文件行中的任何内容)
  • "match" 是 "re.findall" 返回的值。如果它与正则表达式匹配,则它是一个列表,其中包含一行中的所有匹配项(在这种情况下,一行中只有一个)。 "!=" 只是一个运算符,它只是转换为“不等于”,在这种情况下是一个空列表,这意味着它只会附加包含正则表达式的匹配项。
  • 我强烈建议查看 python 文档:docs.python.org/3,尤其是初学者指南:docs.python.org/3.7/tutorial/index.html,它确实帮助了我一段时间。如果在这些页面中找不到您要查找的内容,请尝试在索引模块中搜索:docs.python.org/3.7/py-modindex.html
  • 我使用 map 函数将值从字符串映射到浮点数进行了编辑
猜你喜欢
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-17
  • 1970-01-01
相关资源
最近更新 更多