2014-11-24 196 views
0

我的數據看起來像下面寫入數據到CSV文件的Python

['[\'Patient, A\', \'G\', \'P\', \'RNA\']'] 

不論括號,引號和反斜線的,我想通過對數據進行分離「」和寫入到CSV文件像下面

Patient,A,G,P,RNA 

提起分隔符= '' 做沒有任何幫助。輸出文件然後看起來像

['Patient, A','G','P','RNA'] 

所有在一個單元中。我想將它們分成多列。我怎樣才能做到這一點?

編輯 - 提起quotechar='|'把它們分成不同的單元,但現在看起來

|['Patient, A','G','P','RNA']| 

編輯 -

out_file_handle = csv.writer(out_file, quotechar='|', lineterminator='\n', delimiter = ",") 
data = ''.join(mydict.get(word.lower(), word) for word in re.split('(\W+)', transposed)) 
data = [data,] 
out_file_handle.writerow(data) 

換位:

['Patient, A','G','P','RNA'] 

數據:

['[\'Patient, A\', \'G\', \'P\', \'RNA\']'] 

它有多行,上面是整個數據中的一行。

+2

東西就會讓你開始:https://docs.python.org/2/library/csv.html#csv.writer – BorrajaX 2014-11-24 00:34:06

+0

@BorrajaX謝謝。這就是我的嘗試。我嘗試了多種方法,提到方言,分隔符,行列式,沒有任何工作。也許我看起來有點過頭了。如果你能幫我解決問題,我會很感激。 – abn 2014-11-24 00:37:01

+1

你能編輯你的問題來顯示你的代碼更具體的例子嗎?也許我們會發現一些東西? Thx – BorrajaX 2014-11-24 00:37:59

回答

1

您首先需要讀取該數據轉換爲Python陣列,通過處理字符串作爲存儲CSV文件:

from StringIO import StringIO 
import csv 
data = ['[\'Patient, A\', \'G\', \'P\', \'RNA\']'] 
clean_data = list(csv.reader(StringIO(data[0]))) 

但輸出仍是一個字符串,因爲它甚至不是一個良好形成CSV!在這種情況下,最好的辦法可能是過濾掉所有這些垃圾字符?

import re 
clean_data = re.sub("[\[\]']","",data[0]) 

現在數據[0]是'Patient, A, G, P, RNA'這是一個乾淨的CSV你可以直接寫入到文件中。

+0

爲什麼在'string.replace()'時也會使用're'?它更快,更清晰。 – will 2014-11-24 00:47:15

+0

執行're'引擎單次傳遞的速度要比'string.replace()'引擎的三次傳遞快得多,以去除所有不同的垃圾字符:''''''''和','(可能更多,如果有其他例子)。 – 2014-11-24 00:49:39

+0

如果我們可以確定'[]'字符在開始和結尾,那麼你是正確的,代碼可以是:'clean_data = data [0] [1:-1] .replace('\'', '')' – 2014-11-24 00:50:41

1

Python有一個CSV編寫器。開始了與

import csv 

然後嘗試這樣的事情

with open('new.csv', 'wb') as write_file: 
    file_writer = csv.writer(write_file) 
    for i in range(data): 
     file_writer.writerow([x for x in data[i]]) 

編輯:

您可能需要寫之前先纏鬥的數據位,因爲它看起來像它的字符串,實際上不是一個清單。試着玩弄分裂()函數

list = data.split() 
+0

謝謝。但它實際上是一個列表。 – abn 2014-11-24 01:08:55

1

如果你想要做的是在['[\'Patient, A\', \'G\', \'P\', \'RNA\']'],在那裏你有這些字符串數組,以文件的形式寫入數據,那麼它是一個真正的問題分兩部分。

第一,你是如何將數據分成正確的格式,然後是把它寫入文件。

如果這是你的數據的形式,每一行,那麼這樣的事情應該工作(它進入正確的格式):

data = ['[\'Patient, A\', \'G\', \'P\', \'RNA\']', ...] 
newData = [entry.replace("\'", "")[1:-1].split(",") for entry in data] 

,這將使你在下面的表格數據:

[["Patient", "A", "G", "P", "RNA"], ...] 

然後你可以按照其他答案中的建議將它寫入文件;

with open('new.csv', 'wb') as write_file: 
    file_writer = csv.writer(write_file) 
    for dataEntry in range(newData): 
    file_writer.writerow(dataEntry) 

如果你不真正關心在這一輪使用數據,只想把它清理乾淨,那麼你可以做data.replace("\'", "")[1:-1],然後寫這些字符串到文件。

[1:-1]位只是刪除前後方括號。

0
""" 
          SAVING DATA INTO CSV FORMAT 
    * This format is used for many purposes, mainly for deep learning. 
    * This type of file can be used to view data in MS Excel or any similar 
     Application 
""" 
# == Imports =================================================================== 

import csv 
import sys 

# == Initialisation Function =================================================== 

def initialise_csvlog(filename, fields): 
    """ 
    Initilisation this function before using the Inserction function 

    * This Function checks the data before adding new one in order to maintain 
     perfect mechanisum of insertion 
    * It check the file if not exists then it creates a new one 
    * if it exists then it proceeds with getting fields 

    Parameters 
    ---------- 
    filename : String 
     Filename along with directory which need to be created 
    Fields : List 
     Colomns That need to be initialised 

    """ 
    try : 
     with open(filename,'r') as csvfile: 
      csvreader = csv.reader(csvfile) 
      fields = csvreader.next() 
      print("Data Already Exists") 
      sys.exit("Please Create a new empty file") 
      # print fields 
    except : 
     with open(filename,'w') as csvfile: 

      csvwriter = csv.writer(csvfile) 
      csvwriter.writerow(fields) 

# == Data Insertion Function =================================================== 

def write_data_csv(filename, row_data): 
    """ 
    This Function save the Row Data into the CSV Created 
    * This adds the row data that is Double Listed 

    Parameters 
    ---------- 
    filename : String 
     Filename along with directory which need to be created 
    row_data : List 
     Double Listed consisting of row data and column elements in a list 
    """ 
    with open(filename,'a') as csvfile: 

     csvwriter = csv.writer(csvfile) 
     csvwriter.writerows(row_data) 

if __name__ == '__main__': 
    """ 
    This function is used to test the Feature Run it independently 

    NOTE: DATA IN row_data MUST BE IN THE FOLLOWING DOUBLE LISTED AS SHOWN 
    """ 
    filename = "TestCSV.csv" 
    fields = ["sno","Name","Work","Department"] 
    #Init 
    initialise_csvlog(filename,fields) 
    #Add Data 
    row_data = [["1","Jhon","Coder","Pythonic"]] 
    write_data_csv(filename,row_data) 

# == END ======================================================================= 

讀取模塊和可以開始使用CSV和Excel視圖數據或任何類似的應用程序(在LibreOffice的計算值)

注意:記住放置數據的列表被雙列爲所示在__main__功能(ROW_DATA)