Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何填充支持字典键的多行值的python dictionary对象_Python_Dictionary - Fatal编程技术网

如何填充支持字典键的多行值的python dictionary对象

如何填充支持字典键的多行值的python dictionary对象,python,dictionary,Python,Dictionary,我正在使用python并读取“.txt”文件,并在“Key:value”对中填充python字典,如下所示 示例文件的内容如下所示: product: osName customers: verizon class: CL1 file_location: /opt/test.txt Remarks: This is multi line data. This is line - 1 of remarks field. This is line - 2 of remarks field.

我正在使用python并读取“.txt”文件,并在“Key:value”对中填充python字典,如下所示

示例文件的内容如下所示:

product: osName
customers: verizon    
class: CL1
file_location: /opt/test.txt
Remarks: This is multi line data. This is line - 1 of remarks field. 
This is line - 2 of remarks field. 
This is line - 3 of remarks field. 
category: software
with open(txt_file) as f:
  for l in f:
    key_value = l.strip().split(':',1) 
    txtdict[key_value[0].strip()] = key_value[1].strip()
      
我从文件中读取并填充python字典的代码如下所示:

product: osName
customers: verizon    
class: CL1
file_location: /opt/test.txt
Remarks: This is multi line data. This is line - 1 of remarks field. 
This is line - 2 of remarks field. 
This is line - 3 of remarks field. 
category: software
with open(txt_file) as f:
  for l in f:
    key_value = l.strip().split(':',1) 
    txtdict[key_value[0].strip()] = key_value[1].strip()
      
只要键及其值仅在一行中,上述解决方案就可以正常工作

想知道如何填充python dictionary对象,该对象可以支持任何给定键的多行值


谢谢你的回答。想知道为什么这个问题被标记为非焦点问题。

在获得关键值后添加if语句

with open(txt_file) as f:
  for l in f:
    key_value = l.strip().split(':',1)
    if len(key_value) > 1:
        txtdict[key_value[0].strip()] = key_value[1].strip()
    else:
        txtdict[key_value[0].strip()] = None

在获得密钥值后添加if语句

with open(txt_file) as f:
  for l in f:
    key_value = l.strip().split(':',1)
    if len(key_value) > 1:
        txtdict[key_value[0].strip()] = key_value[1].strip()
    else:
        txtdict[key_value[0].strip()] = None

使用
csv.reader
模块和特定检查准确显示:

import csv

with open('test.txt') as f:
    reader = csv.reader(f, delimiter=':', skipinitialspace=True)
    res = {}
    prev_key = ''
    for row in reader:
        if len(row) == 2:
            k, v = row
            res[k] = v
            prev_key = k
        else:
            res[prev_key] += f'\n{row[0]}'

    print(res)
输出:

{'Remarks': 'Line one\nLine 2 \nLine 3 of Remarks.',
 'category': 'software',
 'class': 'CL1',
 'customers': 'verizon',
 'file_location': '/opt/test.txt',
 'product': 'osName'}

使用
csv.reader
模块和特定检查准确显示:

import csv

with open('test.txt') as f:
    reader = csv.reader(f, delimiter=':', skipinitialspace=True)
    res = {}
    prev_key = ''
    for row in reader:
        if len(row) == 2:
            k, v = row
            res[k] = v
            prev_key = k
        else:
            res[prev_key] += f'\n{row[0]}'

    print(res)
输出:

{'Remarks': 'Line one\nLine 2 \nLine 3 of Remarks.',
 'category': 'software',
 'class': 'CL1',
 'customers': 'verizon',
 'file_location': '/opt/test.txt',
 'product': 'osName'}

你试过什么吗?我看不出有什么实际的问题。多行没有被当作值。这就是问题所在。你试过什么吗?我看不出有什么实际的问题。多行没有被当作值。这就是问题所在。我在这一行遇到语法错误:文件“test1.py”,第69行res[prev_key]+=f'\n{row[0]}'^syntaxer:无效syntax@Ammad,在扫描字符串文字$python3 test1.py文件“test1.py”时检查Python版本syntaxerror:EOL,第69行res[prev_key]+=f'\n{row[0]}“^SyntaxError:无效语法$python3--版本Python 3.5.2当我从此语句中删除“f”时,它可以工作。res[prev_key]+='\n{row[0]}'谢谢您的回答。不知道为什么这个问题被标记为非焦点问题。我在这一行遇到语法错误:文件“test1.py”,第69行res[prev_key]+=f'\n{row[0]}'^SyntaxError:无效syntax@Ammad,在扫描字符串文字$python3 test1.py文件“test1.py”时检查Python版本syntaxerror:EOL,第69行res[prev_key]+=f'\n{row[0]}'^SyntaxError:无效语法$python3--版本python3.5.2当我从此语句中删除“f”时,它可以工作。res[prev_key]+='\n{row[0]}'谢谢您的回答。想知道为什么这个问题被标记为非焦点问题。