2015-06-17 100 views
-1

我的文件被格式化成数字的三列:如何将文本文件转换为字典并打印出来?

2 12345 1.12345 
1 54321 1.54321 
3 12345 1.12345 

我想使用Python的前两列作为键并使用第三列的值。该文件很大,这意味着我无法手工格式化。那么,如何让Python自动将我的大文件转换为字典?

这里是我的代码:

with open ('file name.txt' 'r') as f: 
    rows = (line.split('\t') for line in f) 
    d = { row[0]:row[:3] for row in rows} 
    print(d) 

输出打印的数字斜所有的地方。我如何正确格式化?

+0

它看起来像样品的第一行会创建' “2”'的一个键和一个'['2','12345','1.12345']的值。那是你的意图吗?就打印而言,将字典发送到标准'print()'函数不会自动对其进行格式化,因此您必须决定如何打印并实施它。 – TigerhawkT3

+1

你可以给出所需输出的样本吗? –

+0

[pprint](https://docs.python.org/3/library/pprint.html)可能可以帮助一些格式化。 –

回答

0

你应该尝试 -

import pprint 
d = {} 
with open ('file name.txt','r') as f: 
    for line in f: 
     row = line.split('\t') 
     if len(row) == 3: 
      d[(row[0], row[1])] = row[2] 
pp = pprint.PrettyPrinter(indent=4) 
pp.pprint(d) 
+1

这将不会打印字典比任何更加整齐的OP(我不知道它是他们想要的字典结构)。 – TigerhawkT3

+0

的确如此,但它确实解决了OP在代码中的逻辑问题,至少根据他的要求 –

+0

我认为使用pprint是他的问题的答案。格式化字典并在屏幕上正确输出**。 –

0

香蕉,你靠近。

  1. 您需要用逗号分隔open的参数。
  2. 您想分配row的第三位成员,即row[2]
  3. 您需要决定如何将行的前两个成员分组为可哈希键。制作一个元组,即(row[0],row[1])工程。
  4. 逐行打印字典。

尝试:

所有切片
with open('filename.txt','r') as f: 
    rows = (line.split('\t') for line in f) 
    d = { (row[0],row[1]):row[2] for row in rows} 
for key in d.keys(): 
    print key,d[key]  
+0

我怀疑问题1是一个复制粘贴错误,因为如果他们在他们的程序中有这样的情况,我认为他们会得到一个回溯说“filename.txtr”不存在。 – TigerhawkT3

+0

顺便说一下,这种方法包括换行符'\ n'作为每个字典值的最后一个字符。为避免出现这种情况,可以在'rows =(line.strip()。split('\ t')中使用'strip()'作为f中的行或'd = {(row [0],row [1 ]):row [2] .strip()for row in rows}'。虽然很好的字典理解。 – rjonnal

+0

你能解释为什么我不断收到错误“IndexError:列表索引超出范围?”它是否与我的文件有关。如果是这样,什么是错的? @rjonnal –

0

首先是wrong.You可以line[:2]和第3与line[2]得到的第一个拖列。

而且你不需要在分开的数据结构来创建你行,你可以一个dict comprehension内使用拆箱作业和map功能:

with open ('ex.txt') as f: 
     d={tuple(i):j.strip() for *i,j in map(lambda line:line.split('\t'),f)} 

print(d) 

结果:

{('2', '12345'): '1.12345', ('3', '12345'): '1.12345', ('1', '54321'): '1.54321'} 

注意,如*i是一个列表,并且列表是不可用的对象,您不能将其用作字典键,因此您可以将它转换为tuple

如果你想保留的顺序,你可以使用collections.OrderedDict

from collections import OrderedDict 
with open ('ex.txt') as f: 
     d=OrderedDict({tuple(i):j.strip() for *i,j in map(lambda line:line.split('\t'),f)}) 

print(d) 
OrderedDict([(('2', '12345'), '1.12345'), (('1', '54321'), '1.54321'), (('3', '12345'), '1.12345')]) 
+0

你能帮我吗?你的输出是我想要的输出。但是,你能详细说明你的解释吗? –

+0

@ Banana0101什么部分不明确? – Kasramvd

+0

我插入了上例中的部分内容,并在输出中有标点符号@Kasra –

0

我不知道你到底怎么想的按键布局。无论如何,你应该使用csv模块,使用'\t'作为你的分隔符。

import csv 

with open('data.txt') as file: 
    tsvfile = csv.reader(file, delimiter='\t') 
    d = { "{},{}".format(row[0], row[1]): row[2] for row in tsvfile } 
    print(d) 

打印出: { '3,12345': '1.12345', '1,54321': '1.54321', '2,12345':“1。12345' }

或者,你有这样的:

with open('data.txt') as file: 
    tsvfile = csv.reader(file, delimiter='\t') 
    d = {} 
    for row in tsvfile: 
     d[row[0]] = row[2] 
     d[row[1]] = row[2] 
    print(d) 

打印出:

{'54321': '1.54321', '3': '1.12345', '1': '1.54321', '12345': '1.12345', '2': '1.12345'}