2016-06-19 33 views
0

我有一個文本文件。它的膽量這個樣子/這一切看起來是這樣的(已經被修改。這也不算什麼它最初看起來像)Python-從文本文件製作集合

(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' 
+0

我最近的嘗試是 om_set = set(open('Rye Grass.txt')。read ) –

+1

你需要解析文件的內容 - 'file.read()'只是將所有東西都讀入一個長字符串中,如果你調用'set()',你就得到一組所有字符。爲了解析它,必須知道精確的結構(不僅僅是它的「膽量」)它是否總是三位整數,用逗號分隔? –

+0

它是由逗號分隔的三位整數,它是像素值。 –

回答

1

由於@TimPietzcker建議和信任文件只有逗號分隔三胞胎,用括號括整數的這些固定表示,一氣呵成一個簡單的解析器(OP的問題,也有文件的貪婪「讀」成memors):

#! /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) 

tramsforms:

(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 split與整數轉換元組一起提供給一個集合。

在使用eval/exec這種反序列化任務之前,我真的會考慮兩次。

通過評論從OP建議更新(請更新的問題!):

  1. 在OP的網站上的文件似乎是太大打印(保存在內存中)?
  2. 這是寫的,因爲有問題的廣告...

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

對於理論上乾淨的3-int-tuple轉儲文件來說,這個答案是有效的(如果不是太大,一次加載並映射到一個集合)。

對於具體的任務,我會更新的答案是否有足夠的新的信息已經被添加到這個問題;-)

的一種方式,如果三聯「線」從以前的階段是CONCAT帶或不帶換行符分離,但alwayss缺少逗號,改變文件讀取部之一:

  1. 到基於線讀卡器(當新行分開)和拉組代入循環總是做出新的收穫設定的工會現存的(積累的)像s = s | fresh,正在解決他們在「隔離」

,或者如果這些「塊」被添加像這樣(0, 1, 230)(13, ...)(「打硬」:

  • 修改內部讀取器和代替的現有代碼: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) 
    

    現在解析這個文件並首先轉儲該集合的長度(和元素一樣s排序):

    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個獨特元素作爲輸出,所以我想現在它按照設計工作;-)

    +0

    這是一個很大的設置文件...我不能打印它...如果我只是說「len(on_set)」而不是測試它? –

    +0

    Traceback(最近調用最後一次): File 「C:\ Users \ Dave \ Desktop \ Weed Hunter.py」,第14行, for z in tup.split(','))for tup in split_pits) 文件「C:\ Users \ Dave \ Desktop \ Weed Hunter.py「,第14行, for z in tup.split(','))for tup in split_pits) 文件」C:\ Users \ Dave \ Desktop \ Weed Hunter.py「,第14行, for z in tup.split(','))for tup in split_pits) ValueError:無效文字爲int()與基數10:'230)(1' –

    +0

    所以看起來,文本的內容具有整數像素行程的文件讓我們不像建議的一樣寫;-) ...當然這個新的信息,說這是一個很大的文件(與內存或打印相比),你總是可以減少extracte信息,通過應用len()組。這取決於你想用存儲在集合中的不相交的RGB三元組做什麼? – Dilettant

    0

    只需要小modification.You可以試試這個。

    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)]) 
    
    +0

    我得到一個IOError:[Errno 2]沒有這樣的文件或目錄:但文件肯定存在 –

    +0

    Dw。它開始監聽,現在它說元組對象不可調用? –

    +0

    你從哪一行代碼得到的? – cdarke