简体   繁体   English

如何读取文件并将它们存储在字典中。 不使用 zip

[英]How to read a file and store them in dictionary. without using zip

I need to read the file that contains first line as a key and the second line as a value.我需要读取包含第一行作为键和第二行作为值的文件。 I am able to open the file and read it but i am not able to assign it to the dictionary format.我能够打开文件并阅读它,但我无法将其分配给字典格式。

def ticker(n):
infile = open(n)
content = infile.readlines()
c = {}
for lines in content:
    print (lines)

Below is my output, but i am not able to assign the first line to key and second line to value.下面是我的输出,但我无法将第一行分配给键,将第二行分配给值。

WHOLE FOODS MARKET
WFMI
WYNN RESORTS, LTD
WYNN
XILINX
XLNX
XM SATELLITE RADIO HOLDINGS
XMSR
YAHOO
YHO

Thanks.谢谢。

Use a dict generator:使用字典生成器:

{content[i]:content[i+1] for i in range(0, len(content)-1, 2)}

In your code like so:在您的代码中,如下所示:

def ticker(n):
    infile = open(n)
    content = infile.readlines()
    infile.close()  # Remember to close your file objects... (or use with block)
    return {content[i].strip():content[i+1].strip() for i in range(0, len(content)-1, 2)}

Or, as @ShadowRanger suggests using zip and slices:或者,正如@ShadowRanger 建议使用 zip 和 slices:

def ticker(n):
    with open(n) as infile:
        content = infile.readlines()
        return dict(zip(content[::2], content[1::2]))

You'd use zip , but not with tee , since that would pair all lines with their next line, not even lines with odd.您将使用zip ,但不使用tee ,因为这会将所有行与下一行配对,而不会将偶数行与奇数配对。 To pair even with odd, do:要偶数与奇数配对,请执行以下操作:

def ticker(file_name):
   with open(file_name) as f:
      stripped = iter(map(str.rstrip, f))
      return dict(zip(*([stripped] * 2)))

This is just wrapping a dict constructor around an inlined version of the grouper itertools recipe , though using zip since we know it's paired up.这只是一个包裹dict各地的内联版本构造grouper itertools配方,虽然使用zip ,因为我们知道它的配对。

暂无
暂无

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

相关问题 如何从文本文件读取值并将其存储到字典中。 [蟒蛇] - How to read and store values from a text file into a dictionary. [python] zip 将两个不相等的列表放入一个字典中。 如果 key 没有值,则不给出。 忽略没有键的值 - zip two unequal lists into a dictionary. give none if key has no value. ignore value without a key 我有包含字典键的列表。 如何使用这些键动态访问字典 - I have list which have keys of dictionary. How to access the dictionary using these keys dynamically 使用字典更改字符串中的单词。 蟒蛇 - Changing words in a string using dictionary. python 如何使用 Python 在不解压(压缩格式)的情况下读取 zip 文件内容 - How to read zip file content without unzipping (in compressed format) using Python 读取文本文件并存储到字典中 - Read text file and store into dictionary 如何在不使用 json 库或没有任何其他库的情况下读取 json 文件到字典或列表中? - how to read json file without using json libarary or without any other libarary in to dictionary or list? 如何将 Network.webSocketFrameReceived 保存到 Python 字典。 使用 Pyppeteer 库进行 UI 测试 - How to save Network.webSocketFrameReceived to Python dictionary. Using Pyppeteer library for UI test 字典不接受所有项目。 只接受最后一项。如何将所有值存储在字典中? - All the items are not taken in by the dictionary. Only the last item is taken in. How do I store all the values in the dict? 如何从文本文件读取并将多个值存储到字典中的键 - How to read from a text file and store multiple values to a key in dictionary
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM