拆分并解析(到新文件)字符串,每个第n个字符迭代起始字符 - python

时间:2016-08-08 16:20:35

标签: python loops parsing

我在previous post中问了一个更普遍的方法解决这个问题,但是我一直试图将结果解析为单个文件。我想迭代一个长字符串,从位置1(python 0)开始,每100个字符打印一次。然后,我想移动一个角色并从位置2(python 1)开始并重复该过程,直到我达到最后100个字符。我想将每个“100”行块解析成一个新文件。以下是我目前正在使用的内容:

seq = 7524       # I get this number from a raw_input 
read_num=100

for raw_reads in range(100):
    def nlength_parts(seq,read_num):
        return map(''.join,zip(*[seq[i:] for i in range(read_num)]))


f = open('read' + str(raw_reads), 'w')
f.write("read" '\n')
f.write(nlength_parts(seq,read_num))
f.close

我现在经常遇到的错误

f.write(nlength_parts(seq,read_num))
TypeError: expected a character buffer object

有一些问题,我们将非常感谢任何帮助!

经过一些帮助,我做了一些改动,但仍然无法正常工作:

seq = 7524       # I get this number from a raw_input 
read_num=100

def nlength_parts(seq,read_num):
    return map(''.join,zip(*[seq[i:] for i in range(read_num)]))

for raw_reads in range(100):   # Should be gene length - 100
    f = open('read' + str(raw_reads), 'w')
    f.write("read" + str(raw_reads))
    f.write(nlength_parts)
    f.close

我可能遗漏了一些重要的变量和定义,以保持我的帖子简短,但它引起了混乱。我在下面粘贴了我的全部代码。

#! /usr/bin/env python

import sys,os
import random
import string

raw = raw_input("Text file: " )

with open(raw) as f:
    joined = "".join(line.strip() for line in f)
    f = open(raw + '.txt', 'w')
    f.write(joined)
    f.closed

seq = str(joined)
read_num = 100

def nlength_parts(seq,read_num):
    return map(''.join,zip(*[seq[i:] for i in range(read_num)]))

for raw_reads in range(100):   # ideally I want range to be len(seq)-100
    f = open('read' + str(raw_reads), 'w')
    f.write("read" + str(raw_reads))
    f.write('\n')
    f.write(str(nlength_parts))
    f.close

2 个答案:

答案 0 :(得分:2)

一些事情:

  1. 您可以在全局范围内定义变量seqread_num,然后在函数中使用相同的参数。您应该做的是让函数定义中的参数名称不同,然后在调用它们时将这两个变量传递给函数。
  2. 当你调用nlength_parts时,你不会传递你用它定义的参数中的任何一个,你也缺少()。与#1一起修复此问题。
  3. 您似乎无法定义您正在切割的字符串。您在函数中对seq进行了切片,但seq是代码中的整数。 seq是您在评论中谈论的文件的已处理输出吗?如果是这样,你的实际代码中的它会大得多吗?
  4. 话虽如此,我相信这段代码会做你想做的事情:

    def nlength_parts(myStr, length, paddingChar=" "):
        if(len(myStr) < length):
            myStr += paddingChar * (length - len(myStr))
        sequences = []
        for i in range(0, len(myStr)-length + 1):
        sequences.append(myStr[i:i+length])
        return(sequences)
    foo = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    nlengthfoo = nlength_parts(foo, 10)
    for x in range(0, length(nlengthfoo):
        with open("read" + (x+1), "w") as f:
            f.write(nlengthfoo[x])
    

    编辑:道歉,改变了我的代码以回应你的评论。

答案 1 :(得分:0)

编辑以回应澄清评论:

基本上,您需要一个滚动窗口的字符串。说long_string = "012345678901234567890123456789..."总长度为100.

In [18]: long_string
Out[18]: '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789'

In [19]: window = 10

In [20]: for i in range(len(long_string) - window +1):
   .....:     chunk = long_string[i:i+window]
   .....:     print(chunk)
   .....:     with open('chunk_' + str(i+1) + '.txt','w') as f:
   .....:         f.write(chunk)
   .....:         
0123456789
1234567890
2345678901
3456789012
4567890123
5678901234
6789012345
7890123456
8901234567
9012345678
0123456789
1234567890
2345678901
3456789012
4567890123
5678901234
6789012345
7890123456
8901234567
9012345678
0123456789
1234567890
2345678901
3456789012
4567890123
5678901234
6789012345
7890123456
8901234567
9012345678
0123456789
1234567890
2345678901
3456789012
4567890123
5678901234
6789012345
7890123456
8901234567
9012345678
0123456789
1234567890
2345678901
3456789012
4567890123
5678901234
6789012345
7890123456
8901234567
9012345678
0123456789
1234567890
2345678901
3456789012
4567890123
5678901234
6789012345
7890123456
8901234567
9012345678
0123456789
1234567890
2345678901
3456789012
4567890123
5678901234
6789012345
7890123456
8901234567
9012345678
0123456789
1234567890
2345678901
3456789012
4567890123
5678901234
6789012345
7890123456
8901234567
9012345678
0123456789
1234567890
2345678901
3456789012
4567890123
5678901234
6789012345
7890123456
8901234567
9012345678
0123456789

最后,

In [21]: ls
chunk_10.txt  chunk_20.txt  chunk_30.txt  chunk_40.txt  chunk_50.txt  chunk_60.txt  chunk_70.txt  chunk_80.txt  chunk_90.txt
chunk_11.txt  chunk_21.txt  chunk_31.txt  chunk_41.txt  chunk_51.txt  chunk_61.txt  chunk_71.txt  chunk_81.txt  chunk_91.txt
chunk_12.txt  chunk_22.txt  chunk_32.txt  chunk_42.txt  chunk_52.txt  chunk_62.txt  chunk_72.txt  chunk_82.txt  chunk_9.txt
chunk_13.txt  chunk_23.txt  chunk_33.txt  chunk_43.txt  chunk_53.txt  chunk_63.txt  chunk_73.txt  chunk_83.txt
chunk_14.txt  chunk_24.txt  chunk_34.txt  chunk_44.txt  chunk_54.txt  chunk_64.txt  chunk_74.txt  chunk_84.txt
chunk_15.txt  chunk_25.txt  chunk_35.txt  chunk_45.txt  chunk_55.txt  chunk_65.txt  chunk_75.txt  chunk_85.txt
chunk_16.txt  chunk_26.txt  chunk_36.txt  chunk_46.txt  chunk_56.txt  chunk_66.txt  chunk_76.txt  chunk_86.txt
chunk_17.txt  chunk_27.txt  chunk_37.txt  chunk_47.txt  chunk_57.txt  chunk_67.txt  chunk_77.txt  chunk_87.txt
chunk_18.txt  chunk_28.txt  chunk_38.txt  chunk_48.txt  chunk_58.txt  chunk_68.txt  chunk_78.txt  chunk_88.txt
chunk_19.txt  chunk_29.txt  chunk_39.txt  chunk_49.txt  chunk_59.txt  chunk_69.txt  chunk_79.txt  chunk_89.txt
chunk_1.txt   chunk_2.txt   chunk_3.txt   chunk_4.txt   chunk_5.txt   chunk_6.txt   chunk_7.txt   chunk_8.txt

原始回复

我只是将字符串视为文件。这样可以避免任何切片问题并且非常简单,因为文件API可以让您轻松地“读取”块。

In [1]: import io

In [2]: long_string = 'a'*100 + 'b'*100 + 'c'*100 + 'e'*88

In [3]: print(long_string)
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccceeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

In [4]: string_io = io.StringIO(long_string)

In [5]: chunk = string_io.read(100)

In [6]: chunk_no = 1

In [7]: while chunk:
   ....:     print(chunk)
   ....:     with open('chunk_' + str(chunk_no) + '.txt','w') as f:
   ....:         f.write(chunk)
   ....:     chunk = string_io.read(100)
   ....:     chunk_no += 1
   ....:     
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

注意,我正在使用ipython终端,所以你可以在解释器会话中使用终端命令!

In [8]: ls chunk*
chunk_1.txt  chunk_2.txt  chunk_3.txt  chunk_4.txt

In [9]: cat chunk_1.txt
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
In [10]: cat chunk_2.txt
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
In [11]: cat chunk_3.txt
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
In [12]: cat chunk_4.txt
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
In [13]: