inconsistent indentation with Python after split

北慕城南 提交于 2019-12-11 07:43:36

问题


edit in progress will re-submit sometimes later edit in progress will re-submit sometimes later edit in progress will re-submit sometimes later


回答1:


That should work:

import re #Regex may be the easiest way to split that line

with open(infile) as in_f, open(outfile,'w') as out_f:
    f = (i for i in in_f if i.rstrip()) #iterate over non empty lines
    for line in f:
        _, k = line.split('\t', 1)
        x = re.findall(r'^1..100\t([+-])chr(\d+):(\d+)\.\.(\d+).+$',k)
        if not x:
            continue
        out_f.write(' '.join(x[0]) + '\n')



回答2:


Your question is probably has been answered by others but since you are dealing with very large file, you should use generator method to input line by line.

See this question for details. Lazy Method for Reading Big File in Python?




回答3:


You can use .strip() to remove any whitespace around an item before entering it. This would allow a bit more clarity and solve any indentation issues.

For example:

b=a.split('chr').strip() # No white space either side now
c=b[1].split(':').strip() # No white space
d=c[1].split('..').strip()
e=b[0]+'\t'+c[0]+'\t'+d[0]+'\t'+d[1]+'\t'+'\n'
rfh.write(e)

What this will have done is remove any existing whitespace, and let only your \t's exist.




回答4:


Why not use a regex split ?

import re
with open(<infile>) as inf:
    for annot_info in f:
        split_array = re.split(r'(\W+)(chr\w+):(\d+)..(\d+)', annot_info)
        #do your sql processing here.
        #write out to a file if you wish to.

would give you ['', '+', 'chr6', '140302505', '140302604', '']. You can use the same in your current mysql methods.

PS: The regex pattern I've used would give you empty strings at the beginning and end. Modify the regex or change your sql insert to exclude first and last elements of array while pushing.



来源:https://stackoverflow.com/questions/6673917/inconsistent-indentation-with-python-after-split

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!