簡體   English   中英

在Python中保存CVS文件

[英]saving cvs file in python

我無法將數據保存到csv文件中。 我正在嘗試將該基因中的氨基酸分為非極性(np),極性(p),陰性(neg)和陽性(pos)四類。 我已經找到了如何計算所有單個氨基酸並將其保存到cvs文件中的方法,但是無法找出如何以與單個氨基酸相同的方式保存四個組的數據。

這是保存到csv中的單個氨基酸的代碼:

from Bio import Entrez, SeqIO

Entrez.email = ""

handle = Entrez.efetch(db="nucleotide", id="KT191142", rettype="gb", retmode="text")
record = SeqIO.read(handle, "genbank")
handle.close()

protein_seq = record.seq.translate()
print(protein_seq)

def count_aa(seq, csv_file):

aa_dict = {} # dictionary to store amino acid counts

for aa in seq:

    if aa in aa_dict:
        aa_dict[aa] += 1 # increment the count of an amino acid by 1
    else:
        aa_dict[aa] = 1 # set the count of an amino acid to 1

with open(csv_file, "w") as file:

    aa_list=sorted(aa_dict.keys())

    file.write("amino_acid,count\n")

    for aa in aa_list:
        line = str(aa) + ',' + str(aa_dict[aa]) + '\n'

        file.write(line)

count_aa(protein_seq, "ebola_aa_count2.csv")`

我想將此新代碼保存到一個csv文件中,就像之前的代碼一樣,這是新代碼:

import re

handle = Entrez.efetch(db="nucleotide", id="KT191142", rettype="gb", retmode="text")
records = SeqIO.read(handle, "genbank")
handle.close()

protein_seq = records.seq.translate()
print(protein_seq)

np_count = 0
p_count = 0
neg_count = 0
pos_count = 0

for aa in protein_seq:
    match_np = re.search(r"G|A|V|C|P|L|I|M|W|F", str(aa))
    match_p = re.search(r"S|T|Y|N|Q", str(aa))
    match_neg = re.search(r"D|E", str(aa))
    match_pos = re.search(r"K|R|H", str(aa))
    if match_np:
        np_count += 1
    if match_p:
        p_count += 1
    if match_neg:
        neg_count += 1
    if match_pos:
        pos_count += 1

handle.close()
print(np_count, p_count, neg_count, pos_count)

謝謝您的幫助!

您絕對應該使用熊貓來操作csv文件。

import pandas as pd
df = pd.DataFrame(columns = ['np_count', 'p_count', 'neg_count', 'pos_count'], data =[np_count, p_count, neg_count, pos_count])
df.to_csv('output.csv')

暫無
暫無

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

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