2016-08-02 68 views
-1

我有一個文本文件,它看起來像這樣,在啓動線「距離IQC/E:距離XY」和遵循其餘的是一個長行,直至:END:我有一個文本文件,它是我想分解成字典的一行文本,我該怎麼做?

:BEGIN 
"Distance IQC/E: Distance XY",0.09066,0.09060,0.00040,0.00040,0.00006,,"Distance IQC/F: Distance XY",0.14603,0.14590,0.00080,0.00080,0.00013,,"Distance IQC/G: Distance XY",0.12074,0.12070,0.00080,0.00080,0.00004,,"Distance IQC/I: Distance XY",0.21476,0.21600,0.00200,0.00200,-0.00124,,"Distance IQC/H: Distance XY",0.12714,0.12760,0.00080,0.00080,-0.00046,,"Distance IQC/N: Distance XY",0.08661,0.08690,0.00080,0.00080,-0.00029,,"Distance IQC/M: Distance XY",0.12997,0.13000,0.00080,0.00080,-0.00003, 
:END 

我想知道如何分割這個文本文件,使每一行以「距離」開始,其後的浮動廣告後面是下一個「距離」。

我可以使用file.replace(「:BEGIN」,「」)來擺脫開始和結束。

我要製作字典,然後將該字典重寫爲新的文本文件嗎?

請幫助我是一個非常新的程序員!

編輯:我希望可以將輸出爲:

Distance IQC/E: Distance XY 0.09066 0.09060 0.00040 0.00040 0.00006 
Distance IQC/F: Distance XY 0.14603 0.14590 0.00080 0.00080 0.00013 
Distance IQC/G: Distance XY 0.12074 0.12070 0.00080 0.00080 0.00004 
Distance IQC/I: Distance XY 0.21476 0.21600 0.00200 0.00200 -0.00124 
Distance IQC/H: Distance XY 0.12714 0.12760 0.00080 0.00080 -0.00046 
Distance IQC/N: Distance XY 0.08661 0.08690 0.00080 0.00080 -0.00029 
Distance IQC/M: Distance XY 0.12997 0.13000 0.00080 0.00080 -0.00003 

這樣我可以清晰地歸檔數據轉換成Excel文件或類似的東西。

編輯2:

這裏是代碼的小位我到目前爲止:

with open("file.txt","r") as read_data: 
    f=read_data.read().replace(":BEGIN",'').replace(":END",'') 
+1

你應該閱讀一下[mcve]是什麼 - 如果你想要代碼,你需要向我們展示你到目前爲止的內容,以及哪些內容不起作用。也可能需要包含您所需的輸出。 –

+0

你想要鑰匙是什麼?你想要什麼值?請提供您的預期輸出的例子。 – IanAuld

+0

不需要一個字典來插入換行符:'astr = big_str.replace(「Distance」,「\ nDistance」)' – cdarke

回答

0

考慮到這是你的輸入數據:

line = ':BEGIN\n"Distance IQC/E: Distance XY",0.09066,0.09060,0.00040,0.00040,0.00006,,"Distance IQC/F: Distance XY",0.14603,0.14590,0.00080,0.00080,0.00013,,"Distance IQC/G: Distance XY",0.12074,0.12070,0.00080,0.00080,0.00004,,"Distance IQC/I: Distance XY",0.21476,0.21600,0.00200,0.00200,-0.00124,,"Distance IQC/H: Distance XY",0.12714,0.12760,0.00080,0.00080,-0.00046,,"Distance IQC/N: Distance XY",0.08661,0.08690,0.00080,0.00080,-0.00029,,"Distance IQC/M: Distance XY",0.12997,0.13000,0.00080,0.00080,-0.00003,\n:END' 

然後,如你所說,你可以先擺脫BEGIN和END。

data = line.replace(':BEGIN\n', '') 
data = line.replace(',\n:END', '') 

然後使用兩個逗號分隔其餘數據。

data_list = data.split(',,') 

最後,你可以創建你的字典,用逗號分隔每一行作爲分隔符。列表中的第一個元素可能是鍵(包括本例中的引號)。對於該值,您可以將它們分組到一個列表中。

data_dict = dict() 

for data_element in data_list: 
    element_as_list = data_element.split(',') 
    key = element_as_list[0] 
    value = element_as_list[1:] 

    data_dict[key] = value 
0
str = 'Distance IQC/E: Distance XY",0.09066,0.09060,0.00040,0.00040,0.00006,,"Distance IQC/F: Distance XY",0.14603,0.14590,0.00080,0.00080,0.00013,,"Distance IQC/G: Distance XY",0.12074,0.12070,0.00080,0.00080,0.00004,,"Distance IQC/I: Distance XY",0.21476,0.21600,0.00200,0.00200,-0.00124,,"Distance IQC/H: Distance XY",0.12714,0.12760,0.00080,0.00080,-0.00046,,"Distance IQC/N: Distance XY",0.08661,0.08690,0.00080,0.00080,-0.00029,,"Distance IQC/M: Distance XY",0.12997,0.13000,0.00080,0.00080,-0.00003,' 

sub_str = str.split(',,') 
temp_arr = [] 

for i in sub_str: 
    temp_arr.append(i.split(',')) 

for i in temp_arr: 
    str_i = ' '.join(i) 
    print(str_i) 

這樣就解決了問題。

相關問題