【问题标题】:Python - String split lines [closed]Python - 字符串分割线[关闭]
【发布时间】:2013-05-12 08:28:12
【问题描述】:
add_numbers( "A1", "Element 560234 65952 6598881 20203256 2165883 659562 654981 24120 261240 31648948 23900 5512400 5512900 5612400 5612900" )

add_numbers( "A2", "Element 261240 31659 5612400 76803256 3165883 659863 654224 44120 261240 31648948 23900 3612200 9512900 5612400 5642924" )

add_numbers( "A3", "Element 841225 65952 2165883 63103256 2165883 644861 344966 84120 161540 31653948 23900 5513426 5518906 5682405 8682932" )

我想得到一个字典(从上面的字符串,它是一个 txt 文件),看起来像这样:

{A1: 560234, 65952,6598881, 20203256,2165883, 659562,....}

{A2: 261240 31659 5612400,....}

{A3: 841225 65952 2165883,....}

你有什么想法吗?我怎样才能做到这一点?谢谢。

【问题讨论】:

  • 能否格式化您的代码?
  • 什么是 add_numbers ?
  • 基本上就是string.split()[1:]
  • @tchike 你的意思是add_numbers( "A1", "Element 56023 ...实际上是在一个文本文件中吗?
  • @tchike 啊,那让我重写我的答案。

标签: python string split


【解决方案1】:

现在了解您想要处理这个

add_numbers( "A1", "Element 560234 65952 6598881 20203256 2165883 659562 654981 24120 261240 31648948 23900 5512400 5512900 5612400 5612900" )

add_numbers( "A2", "Element 261240 31659 5612400 76803256 3165883 659863 654224 44120 261240 31648948 23900 3612200 9512900 5612400 5642924" )

add_numbers( "A3", "Element 841225 65952 2165883 63103256 2165883 644861 344966 84120 161540 31653948 23900 5513426 5518906 5682405 8682932" )

作为文本文件的文字内容到字典中,我会这样做:

import re # import regular expression module
d = {}

for line in open("myfile.txt", "r"):
    if not line.strip(): continue        # Skip blank lines
    data = re.findall('"([^"]*)"', line) # Extract text between double quotes

    if len(data) != 2: continue          # There were not exactly two pairs of double quotes, skip this line

    key, value = data
    d[key] = map(int, value.split()[1:]) # Remove "Element" and convert numbers to integers, add to dictionary

正则表达式"([^"]*)"的解释:

  • "( )" 匹配引号内的内容
  • [^"]* 任何不属于" 的 0 个或多个字符的字符串

re.findall 将以列表的形式返回结果。

编辑

我得到一个错误。 ValueError: 需要超过 1 个值才能解压

您的文件中必须有一行不包含两对双引号。我已经更新了上面的代码以忽略与您的规范不匹配的行。

【讨论】:

  • @Joran 很好,我将答案更新为map(int,...) 结果。 +1
  • 我收到一个错误。 ValueError: 需要超过 1 个值才能解压
  • @tchike 添加了一个编辑来回应这个
【解决方案2】:
import re,ast
def add_numbers(d,key,elements): #we pass in a reference to a dict, which we update
    d[key] = map(int,elements.split()[1:]) #Returns ["Element",...], so we select all but first [1:]
dic = {}
with open('file.txt') as f:
    for line in f:
        key,elems = ast.literal_eval(re.search(r'\((.+)\)',line).group(0))
        add_numbers(dic,key,elems)

生产

>>> 
{'A1': [560234, 65952, 6598881, 20203256, 2165883, 659562, 654981, 24120, 261240, 31648948, 23900, 5512400, 5512900, 5612400, 5612900], 'A3': [841225, 65952, 2165883, 63103256, 2165883, 644861, 344966, 84120, 161540, 31653948, 23900, 5513426, 5518906, 5682405, 8682932], 'A2': [261240, 31659, 5612400, 76803256, 3165883, 659863, 654224, 44120, 261240, 31648948, 23900, 3612200, 9512900, 5612400, 5642924]}

【讨论】:

  • 我不能使用这个,因为 add_numbers("A1", "Element 56023....在一个txt文件中,我必须先拆分。
  • @tchike 如果您发布此类文件内容的示例,我将更新我的解决方案。
  • txt文件的内容是我问题的第一部分
  • @tchike 查看我的更新答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-10
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 2012-03-03
  • 2021-06-12
  • 1970-01-01
相关资源
最近更新 更多