Python 2.7 python 2.7如何将结果写入xlsx文件
我有几句话: ATCAAGTCCGTAGTACGGGAATGCAAAAAA gggctaggtaggattgcctagtcaactggggggg tagctagtagggattgcctagtcaactggcccgg 现在,我想在每个读写文件的左边剪切12个碱基:Python 2.7 python 2.7如何将结果写入xlsx文件,python-2.7,Python 2.7,我有几句话: ATCAAGTCCGTAGTACGGGAATGCAAAAAA gggctaggtaggattgcctagtcaactggggggg tagctagtagggattgcctagtcaactggcccgg 现在,我想在每个读写文件的左边剪切12个碱基: f2 = open("./read1.csv","w") with open('../001.fastq') as reader: for index, line in enumerate(reader):
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文件每个读取有4行 @ ATCAAGTCCGTAGTACGGGAATGCAAAAAA + JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ @ gggctaggtaggattgcctagtcaactggggggg + JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ @ tagctagtagggattgcctagtcaactggcccgg + JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ 对于本例,输出为: ATCAAGTCCG gggctagtagg
tagctaggg假设您在Windows上并安装了Excel。 有多个库可以从python中使用Excel。除此之外,我只使用了
win32com.client
,但我对它非常满意。我认为默认情况下,它与anaconda一起提供,但是如果没有,那么您可以从这里下载它:(不要忘记选择合适的版本/架构)。
以下是最重要功能的小参考:
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)
编辑
但是这个问题已经在这里得到了回答:你能显示你想要的输出吗?结果是:atcaaagtccg GGGCTAGGTAGG tagctagtaggar你想问一个XSLX或CSV文件吗?代码是写一个CSV文件,但我想保存xlsx文件,我不知道如何修改它。请按照指南编辑你的原始问题(并删除此答案)。这些额外的“帖子”仅用于回答(评论要求澄清,添加一些补充或非必要信息和类似内容)虽然原始问题是唯一一个与之相关的地方。我知道它可能会让人有点困惑。顺便说一句,欢迎来到stackoverflow。