簡體   English   中英

Python-通過文本文件進行設置

[英]Python- making a set from a text file

我有一個文本文件。 它的膽量看起來像這樣/全部看起來像這樣(已被編輯。這也不是最初看起來的樣子)

 (0, 16, 0)
 (0, 17, 0)
 (0, 18, 0)
 (0, 19, 0)
 (0, 20, 0)
 (0, 21, 0)
 (0, 22, 0)
 (0, 22, 1)
 (0, 22, 2)
 (0, 23, 0)
 (0, 23, 4)
 (0, 24, 0)
 (0, 25, 0)
 (0, 25, 1)
 (0, 26, 0)
 (0, 26, 3)
 (0, 26, 4)
 (0, 26, 5)
 (0, 26, 9)
 (0, 27, 0)
 (0, 27, 1)

無論如何,如何將這些值放入python 2的集合中?

我最近的嘗試是

om_set = set(open('Rye Grass.txt').read()

編輯:這是我用來獲取文本文件的代碼。 導入cv2導入numpy作為np導入時間

om=cv2.imread('spectrum1.png')
om=om.reshape(1,-1,3)
om_list=om.tolist()
om_tuple={tuple(item) for item in om_list[0]}
om_set=set(om_tuple)

im=cv2.imread('1.jpg')        
im=cv2.resize(im,(100,100))         
im= im.reshape(1,-1,3)
im_list=im.tolist()
im_tuple={tuple(item) for item in im_list[0]}
ColourCount= om_set & set(im_tuple)
with open('Weedlist', 'a') as outputfile:
    output = ', '.join([str(tup) for tup in sorted(ColourCount)])
    outputfile.write(output)
print 'done'

im=cv2.imread('2.jpg')        
im=cv2.resize(im,(100,100))         
im= im.reshape(1,-1,3)
im_list=im.tolist()
im_tuple={tuple(item) for item in im_list[0]}
ColourCount= om_set & set(im_tuple)
with open('Weedlist', 'a') as outputfile:
    output = ', '.join([str(tup) for tup in sorted(ColourCount)])
    outputfile.write(output)
print 'done'

正如@TimPietzcker所建議並信任的文件那樣,它們只有用逗號分隔的三元組的整數的固定表示形式,並用括號括起來,是一個簡單的解析器(OP的問題也有貪婪的“讀取”文件成備忘錄):

#! /usr/bin/env python
from __future__ import print_function


infile = 'pixel_int_tuple_reps.txt'
split_pits = None
with open(infile, 'rt') as f_i:
    split_pits = [z.strip(' ()') for z in f_i.read().strip().split('),')]
if split_pits:
    on_set = set(tuple(int(z.strip())
                 for z in tup.split(', ')) for tup in split_pits)

    print(on_set)

有軌電車

(0, 19, 0), (0, 20, 0), (0, 21, 1), (0, 22, 0), (0, 24, 3), (0, 27, 0), (0, 29, 2), (0, 35, 2), (0, 36, 1)

變成:

set([(0, 27, 0), (0, 36, 1), (0, 21, 1), (0, 22, 0), (0, 24, 3), (0, 19, 0), (0, 35, 2), (0, 29, 2), (0, 20, 0)])

小片段:

  1. 分割像素整數三胞胎成的子串0, 19, 0清潔位的雜散括號和空格遠離(也末照顧右括號的。

  2. 如果“可行”,則進一步將帶有整數轉換元組的rgb拆分輸入一組。

在對這種反序列化任務使用eval / exec之前,我會三思而后行。

更新由OP(請更新的問題!)意見的建議:

  1. OP網站上的文件似乎太大而無法打印(保留在內存中)?
  2. 不是書面的,因為有問題的廣告...

...所以直到我們從OP獲得更多信息為止:

對於理論上干凈的3位元組轉儲文件,此答案有效(如果不是太大而無法一次加載並映射到集合中)。

對於具體任務,如果已經將足夠多的新信息添加到問題中,我可以更新答案;-)

一種方法,如果三元“行”與上一階段連接在一起,有或沒有換行符分隔,但始終缺少逗號,則可以更改文件讀取部分:

  1. 放入基於行的閱讀器中(當換行符分開時),並將集合生成拉入循環,始終使新收獲的集合與現有的(累積的)集合(如s = s | fresh 解決“隔離”問題的s = s | fresh事物

或如果像這樣添加這些“塊” (0, 1, 230)(13, ...)( “努力擊中”:

  1. 修改閱讀器內部的現有代碼,而不是: f_i.read().strip().split('),')編寫f_i.read().replace(')('), (', ').strip().split('),') ...就是將)(部分)( “固定”到), (部分,使其能夠繼續下去,就好像它是同質的“結構”一樣。

現在更新以解析數據集的版本2(更新的問題):

文件pixel_int_tuple_reps_v2.txt現在具有:

 (0, 16, 0)
 (0, 17, 0)
 (0, 18, 0)
 (0, 19, 0)
 (0, 20, 0)
 (0, 21, 0)
 (0, 22, 0)
 (0, 22, 1)
 (0, 22, 2)
 (0, 23, 0)
 (0, 23, 4)
 (0, 24, 0)
 (0, 25, 0)
 (0, 25, 1)
 (0, 26, 0)
 (0, 26, 3)
 (0, 26, 4)
 (0, 26, 5)
 (0, 26, 9)
 (0, 27, 0)
 (0, 27, 1)

編碼:

#! /usr/bin/env python
from __future__ import print_function


infile = 'pixel_int_tuple_reps_v2.txt'
on_set = set()
with open(infile, 'rt') as f_i:
    for line in f_i.readlines():
        rgb_line = line.strip().lstrip('(').rstrip(')')
        try:
            rgb = set([tuple(int(z.strip()) for z in rgb_line.split(', '))])
            on_set = on_set.union(rgb)
        except:
            print("Ignored:" + rgb_line)
            pass
print(len(on_set))
for rgb in sorted(on_set):
    print(rgb)

現在解析此文件,並首先轉儲集合和的長度(以及按排序順序排列的元素):

21
(0, 16, 0)
(0, 17, 0)
(0, 18, 0)
(0, 19, 0)
(0, 20, 0)
(0, 21, 0)
(0, 22, 0)
(0, 22, 1)
(0, 22, 2)
(0, 23, 0)
(0, 23, 4)
(0, 24, 0)
(0, 25, 0)
(0, 25, 1)
(0, 26, 0)
(0, 26, 3)
(0, 26, 4)
(0, 26, 5)
(0, 26, 9)
(0, 27, 0)
(0, 27, 1)

HTH。 請注意,提供的樣本輸入中沒有重復項。 將最后一條數據線加倍,我仍然可以接收到21個唯一元素作為輸出,所以我想現在它可以按設計工作;-)

只需要進行少量修改即可。

om_set = set(eval(open('abc.txt').read()))

結果

{(0, 19, 0),
 (0, 20, 0),
 (0, 21, 1),
 (0, 22, 0),
 (0, 24, 3),
 (0, 27, 0),
 (0, 29, 2),
 (0, 35, 2)}

編輯這是IPython提示符中的代碼工作。

In [1]: file_ = open('abc.txt')
In [2]: text_read = file_.read()
In [3]: print eval(text_read)
((0, 19, 0), (0, 20, 0), (0, 21, 1), (0, 22, 0), (0, 24, 3), (0, 27, 0), (0, 29, 2), (0, 35, 2), (0, 36, 1))
In [4]: type(eval(text_read))
Out[1]: tuple
In [5]: print set(eval(text_read))
set([(0, 27, 0), (0, 36, 1), (0, 21, 1), (0, 22, 0), (0, 24, 3), (0, 19, 0), (0, 35, 2), (0, 29, 2), (0, 20, 0)])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM