【问题标题】:python 2.7 How to write the result to the xlsx file [duplicate]python 2.7如何将结果写入xlsx文件[重复]
【发布时间】:2018-09-18 08:54:48
【问题描述】:

我有一些读物:

ATCAAAGTCCCGTAGGTACGGGAAATGCAAAAAAA GGGCTAGGTAGGGATTGCCTAGTCAACTGGGGGGGG TAGCTAGGTAGGGATTGCCTAGTCAACTGGCCCGG

...

...

现在,我想把每个读写左边的12个碱基切到一个文件中:

f2 = open("./read1.csv","w")
with open('../001.fastq') as reader:
     for index, line in enumerate(reader):
         if index % 4 == 1:
             f2.write(line[:12]+'\n')
f2.close()

我想知道怎么写一个xlsx文件

【问题讨论】:

  • 你能显示你想要的输出吗?
  • 输出是:ATCAAAGTCCCG GGGCTAGGTAGG TAGCTAGGTAGG
  • 您询问的是 XSLX 还是 CSV 文件?
  • 代码是写一个csv文件,但是我想保存xlsx文件,不知道怎么修改

标签: python-2.7


【解决方案1】:

每次读取有 4 行

@

ATCAAAGTCCCGTAGGTACGGGAAATGCAAAAAAA

+

JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ

@

GGGCTAGGTAGGGATTGCCTAGTCAACTGGGGGGGG

+

JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ

@

TAGCTAGGTAGGGATTGCCTAGTCAACTGGCCCGG

+

JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ

对于这个例子,输出是:

ATCAAAGTCCCG

GGGCTAGGTAGG

TAGCTAGGTAGG

【讨论】:

  • 请遵守指南并编辑您的原始问题(并删除此答案)。这些额外的“帖子”仅供回答(cmets 要求澄清,添加一些补充或非必要信息和类似内容),而原始问题是唯一一个与它相关的地方。我知道这可能会首先令人困惑。顺便说一句,欢迎使用 stackoverflow。
【解决方案2】:

假设您使用的是 Windows 并安装了 Excel。 有多个库可以使从 python 中使用 Excel 成为可能。其中我只使用过win32com.client,但我很满意。我认为它默认带有 anaconda,但如果没有,您可以从这里下载它:https://github.com/mhammond/pywin32/releases(不要忘记选择适当的版本/架构)。 以下是对最重要功能的简要参考:

from win32com.client import Dispatch  # import the necessary library

xlApp = Dispatch("Excel.Application") # starts excel
xlApp.Visible = False                 # hides window (this makes things a bit faster)
xlBook = xlApp.Workbooks.Open( fullPathToXlsx ) # opens an existing workbook

xlSheet = xlBook.Sheets(3)            # which sheet you want to use, indexing starts from 1!
print [sheet.Name for sheet in xlBook.Sheets] # if it is not the one you want you can see their order this way
xlSheet = xlBook.Sheets(sheetName)    # or you can use its name

xlSheet.Cells(row, col).Value = newValue # newValue is what you want to assign to the cell, row and col indexing starts from 1

xlApp.DisplayAlerts = False           # turns off any affirmation popup
xlBook.Save()                         # saving workbook
xlBook.SaveAs(newFullPath)            # saving as...

xlBook.Close()                        # close workbook
xlApp.Quit()                          # exit application

您可能希望在列/行索引和“字母表示”之间进行转换(我的意思是左上角单元格的“A1”等)。这是我用来做的:

def cellStringToNum(aStr):
    """
    returns an excel sheet cell reference as numbers in a list as [col, row]
    """
    import string

    colS = ""
    for i in xrange(len(aStr)):
        if aStr[i] in string.ascii_uppercase:
            colS += aStr[i]
        else:
            row = int(aStr[i:])
            break
    col = 0
    for i in xrange(len(colS)):
        col += (string.ascii_uppercase.find(colS[len(colS)-1-i])+1)*(26**i)
    return [col, row]

def cellNumToString(col, row):
    import string

    colS = string.ascii_uppercase[col % 26 - 1]
    if col % 26 == 0:
        col -= 26
    else:
        col -= col % 26
    while col != 0:
        col /= 26
        colS = string.ascii_uppercase[col % 26 - 1] + colS
        if col % 26 == 0:
            col -= 26
        else:
            col -= col % 26
    return colS+str(row)

编辑

但是这个问题已经在这里回答了:Python - Write to Excel Spreadsheet

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-26
    • 2016-12-30
    • 2018-06-30
    • 1970-01-01
    • 2021-12-29
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多