简体   繁体   English

将数据写入CSV文件Python

[英]Write data to a csv file Python

My data looks like below 我的数据如下所示

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

Irrespective of the brackets, quotes and back slashes, I'd like to separate the data by ',' and write to a CSV file like below 无论括号,引号和反斜杠如何,我都想用“,”分隔数据,然后将其写入CSV文件,如下所示

Patient,A,G,P,RNA

Mentioning delimiter = ',' has done no help. 提及定界符=','并没有帮助。 The output file then looks like 输出文件如下所示

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

all in a single cell. 全部放在一个单元格中。 I want to split them into multiple columns. 我想将它们分成多列。 How can I do that? 我怎样才能做到这一点?

Edit - Mentioning quotechar='|' 编辑-提及quotechar='|' split them into different cells but it now looks like 将它们分成不同的单元格,但现在看起来像

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

Edit- 编辑-

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)

transposed: 转置:

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

data: 数据:

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

And it has multiple rows, the above is one of the rows from the entire data. 并且它具有多行,以上是整个数据中的行之一。

You first need to read this data into a Python array, by processing the string as a CSV file in memory: 首先,需要通过将字符串作为CSV文件处理在内存中,将此数据读入Python数组中:

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

However the output is still a single string, because it's not even a well-formed CSV! 但是输出仍然是单个字符串,因为它甚至不是格式正确的CSV! In which case, the best thing might be to filter out all those junk characters? 在那种情况下,最好的办法可能是过滤掉所有这些垃圾字符?

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

Now data[0] is 'Patient, A, G, P, RNA' which is a clean CSV you can write straight to a file. 现在data [0]是'Patient, A, G, P, RNA' ,这是干净的CSV,您可以直接将其写入文件。

Python has a CSV writer. Python有一个CSV编写器。 Start off with 从开始

import csv

Then try something like this 然后尝试这样的事情

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

Edit: 编辑:

You might have to wrangle the data a bit first before writing it, since it looks like its a string and not actually a list. 您可能需要先对数据进行一些处理,然后再进行写入,因为它看起来像是字符串,而不是列表。 Try playing around with the split() function 尝试使用split()函数

list = data.split()

If what you're trying to do is write data in the form of ['[\\'Patient, A\\', \\'G\\', \\'P\\', \\'RNA\\']'] , where you have an array of these strings, to file, then it's really a question in two parts. 如果您要执行的操作是以['[\\'Patient, A\\', \\'G\\', \\'P\\', \\'RNA\\']']的形式写入数据,将这些字符串组成的数组归档,那么这实际上是一个分为两个部分的问题。

The first, is how do you separate the data into the correct format, and then the second is is to write it to file. 第一个是如何将数据分离为正确的格式,然后第二个是将其写入文件。

If that is the form of your data, for every row, then something like this should work (to get it into the correct format): 如果那是数据的形式,那么对于每一行,这样的事情都应该起作用(将其转换为正确的格式):

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

that will give you data in the following form: 将以以下形式为您提供数据:

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

and then you can write it to file as suggested in the other answers; 然后您可以按照其他答案中的建议将其写入文件;

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

If you don't actually care about using the data in this round, and just want to clean it up, then you can just do data.replace("\\'", "")[1:-1] and then write those strings to file. 如果您实际上并不关心在本轮中使用数据,而只想清理它,则可以执行data.replace("\\'", "")[1:-1] ,然后将其写入文件字符串。

The [1:-1] bits are just to remove the leading and trailing square brackets. [1:-1]位仅用于删除前和后方括号。

"""
                             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 =======================================================================

Read the Module and you can start using CSV and view data in Excel or any similar application (calc in libreoffice) 阅读该模块,您可以开始使用CSV并在Excel或任何类似应用程序中查看数据(libreoffice中的calc)

NOTE: Remember to place list of data to be double listed as shown in __main__ function (row_data) 注意:请记住要放置要重复列出的数据列表,如__main__函数(row_data)所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM