简体   繁体   English

从具有异常格式的文本文件写入 python 字典

[英]Writing a python dictionary from a text file with an unusual format

I am trying to creat a python dictionary from a text file that is formatted in this way.我正在尝试从以这种方式格式化的文本文件创建 python 字典。 I cannot change it.我无法改变它。

dataname
definition of data
dataname2
definition of data2
dataname3
definition of data3

The data is routinely unspaced.数据通常是无间隔的。 The keys have to be the the dataname and the values have to be the definition of the data.键必须是数据名,值必须是数据的定义。 I know I need readlines() or to keep track of which line is a key and which is a value somehow.我知道我需要 readlines() 或以某种方式跟踪哪一行是键,哪一行是值。 This is what I have.这就是我所拥有的。 Any help is appreciated.任何帮助表示赞赏。

d = {}
with open('dict.txt', 'r') as myFile:
    for line in myFile:
        x = myFile.readlines()
        word = x[0]
        description = x[1]
        d[word] = description
    print(d)

You can iterate on the file with a for loop, which will iterate on the lines, and use the next method of the file object to iterate to the next line:您可以使用for循环对文件进行迭代,该循环将在行上进行迭代,并使用文件 object 的next方法迭代到下一行:

d = {}

with open('dict.txt') as f:
    for line in f:
        name = line.strip()
        description = next(f).strip()
        d[name] = description

print(d)
#{'dataname': 'definition of data', 'dataname2': 'definition of data2', 'dataname3': 'definition of data3'}

You can use zip_longest from itertools to do:您可以使用 itertools 中的zip_longest来执行以下操作:

import itertools
ur_dict={}
with open(ur_file) as f:
    for line1,line2 in itertools.zip_longest(*[f]*2):
        ur_dict[line1.rstrip()]=line2.rstrip()

>>> ur_dict
{'dataname': 'definition of data', 'dataname2': 'definition of data2', 'dataname3': 'definition of data3'}

Which could be a dict comprehension:这可能是一个 dict 理解:

with open('so.txt') as f:
    ur_dict={line1.rstrip():line2.rstrip() 
            for line1,line2 in itertools.zip_longest(*[f]*2)}

Or, you can use zip with the iterator twice:或者,您可以将zip与迭代器一起使用两次:

with open(ur_file) as f:
    ur_dict={l1.rstrip():l2.rstrip() for l1,l2 in zip(f,f)}

Code:代码:

d = {}
file = open('dict.txt', 'r')
text = file.read().strip()
file.close()

textlist = text.split('\n')
for i in range(0, len(textlist), 2):
    colname = textlist[i]
    colvalue = [textlist[i+1]]
    d[colname] = colvalue
#display dictionary
print(d)
df = pd.DataFrame(d)
#display dataframe 
print(df)

I hope it would be helpful.我希望它会有所帮助。

with open('dict.txt', 'r') as file:
    split = file.read().splitlines()
    dictionary = {k:v for k, v in zip(split[::2], split[1::2])}
{'dataname': 'definition of data',
 'dataname2': 'definition of data2',
 'dataname3': 'definition of data3'}

print(file.splitlines()[::2]) 
# >>> ['dataname', 'dataname2', 'dataname3']

print(file.splitlines()[1::2])
# >>> ['definition of data', 'definition of data2', 'definition of data3']

Use slice(start, stop, step) operation to give you odd and even list items.使用slice(start, stop, step)操作为您提供奇数和偶数列表项。

I would do it like below.我会像下面那样做。

  1. Read all the lines into a list with each line being a separate element in list.将所有行读入一个列表,每一行都是列表中的一个单独元素。
  2. Create a sub_list out of main list to be considered as keys .从主列表中创建一个 sub_list 被视为keys This would be alternate elements from main list starting from element 0 .这将是从element 0开始的主列表中的备用元素。
  3. Create a sub_list out of main list to be considered as values .从主列表中创建一个 sub_list 以被视为values This would be alternate elements from main list starting from element 1 .这将是从element 1开始的主列表中的备用元素。
  4. Merge the 2 lists so that 1st element from key_list is mapped with 1st element of value_list.合并 2 个列表,以便 key_list 的第一个元素与 value_list 的第一个元素映射。

Try this:尝试这个:

with open('dict.txt') as f:
    content = f.readlines()

content = [x.strip() for x in content]   # to remove whitespace characters like `\n` at the end of each line

key_list = content[::2]       # every alternate element in list is treated as keys. Starting from 1st element.
value_list = content[1::2]    # every alternate element in list is treated as value. Starting from 2nd element.

final_dict = dict(zip(key_list, value_list))    # merge the 2 lists to create a key, value pair dictionary.

print(final_dict)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM