第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

帶有用戶定義函數(shù)的 Python 打印

帶有用戶定義函數(shù)的 Python 打印

拉風(fēng)的咖菲貓 2021-08-05 17:52:13
我正在嘗試編寫(xiě)一個(gè)代碼,該代碼將從文件中獲取數(shù)據(jù)并以不同的方式編寫(xiě)。我有大部分代碼,但是當(dāng)我運(yùn)行它時(shí),一切都在一行上。import csv#Step 4def read_data(filename):    try:        data = open("dna.txt", "r")    except IOError:        print( "File not found")    return data#Step 5def get_dna_stats(dna_string):    a_letters = ""    t_letters = ""    if "A" in dna_string:        a_letters.append("A")    if "T" in dna_string:        t_letters.append("T")    nucleotide_content = ((len(a_letters) + len(t_letters))/len(dna_string))#Step 6def get_dna_complement(dna_string):    dna_complement = ""    for i in dna_string:        if i == "A":            dna_complement.append("T")        elif i == "T":            dna_complement.append("A")        elif i == "G":            dna_complement.append("C")        elif i == "C":            dna_complement.append("G")        else:            break    return dna_complement#Step 7def print_dna(dna_strand):    dna_complement = get_dna_complement(dna_strand)    for i in dna_strand:        for j in dna_complement:            print( i + "=" + j)#Step 8def get_rna_sequence(dna_string):    rna_complement = ""    for i in dna_string:        if i == "A":            rna_complement.append("U")        elif i == "T":            rna_complement.append("A")        elif i == "G":            rna_complement.append("C")        elif i == "C":            rna_complement.append("G")        else:            break    return rna_complement#Step 9def extract_exon(dna_strand, start, end):    return (f"{dna_strand} between {start} and {end}")#Step 10def calculate_exon_pctg(dna_strand, exons):    exons_length = 0    for i in exons:        exons_length += 1    return exons_length/ len(dna_strand)#Step 11def format_data(dna_string):    x = "dna_strand"[0:62].upper()    y = "dna_strand"[63:90].lower()    z = "dna_strand"[91:-1].upper()    return x+y+z當(dāng)我運(yùn)行它時(shí),它應(yīng)該輸出一個(gè)看起來(lái)像這樣的文件,但我的代碼最終將每個(gè)單詞都像這樣放在第一行 我覺(jué)得它與write_results函數(shù)有關(guān),但這就是我所知道的如何寫(xiě)入文件。我犯的第二個(gè)錯(cuò)誤是我沒(méi)有在 append 語(yǔ)句中正確調(diào)用函數(shù)。我試過(guò)連接,我試過(guò)格式化字符串,但現(xiàn)在我在我需要做的事情上遇到了障礙。
查看完整描述

3 回答

?
有只小跳蛙

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個(gè)贊

當(dāng)您寫(xiě)入文件'\n'時(shí),每次您想在寫(xiě)入的文件的新行上添加某些內(nèi)容時(shí),都需要將 a 連接到字符串的末尾

例如:

output.append("The AT content is" + get_dna_stats() + "% of the DNA sequence." + '\n')

為了解決您的第二個(gè)問(wèn)題,我會(huì)將您的代碼更改為如下所示:

temp = "The AT content is" + get_dna_stats() + "% of the DNA sequence." + '\n'output.append(temp)

當(dāng)您附加到列表并調(diào)用函數(shù)時(shí),它將采用函數(shù)的文字文本而不是調(diào)用它。使用臨時(shí)字符串持有者執(zhí)行此操作將在連接字符串之前調(diào)用該函數(shù)。然后您就可以將字符串附加到列表中


查看完整回答
反對(duì) 回復(fù) 2021-08-05
?
子衿沉夜

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

你從來(lái)沒(méi)有告訴你的程序要換一條新線。您可以在"\n"每個(gè)字符串中附加或預(yù)先添加特殊字符,也可以通過(guò)以下方式以系統(tǒng)不可知的方式進(jìn)行


import os


在文件的頂部并像這樣編寫(xiě) write_results 函數(shù):


def write_results(output, filename):

    try:

        with open("output.csv","w") as csvFile:

            writer = csv.writer(csvFile)

            for i in output:

                csvFile.write(i)

                os.write(csvFile, os.linesep)  # Add this line! It is a system agnostic newline

    except IOError:

        print("Error writing file")


查看完整回答
反對(duì) 回復(fù) 2021-08-05
  • 3 回答
  • 0 關(guān)注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)