开发者

convert string into ASCII code

开发者 https://www.devze.com 2023-03-30 16:31 出处:网络
I need to convert string into ASCII code. I\'m using python. I did as below: b1=[ord(x) for x in l1[i]]

I need to convert string into ASCII code. I'm using python. I did as below:

b1=[ord(x) for x in l1[i]]

here l1 is a linelist,开发者_StackOverflow中文版 l1[i] is the ith line of l1

but I got error like:

Traceback (most recent call last):
  File "./fastq_phred_filter.py", line 24, in ?
    b1=[ord(x) for x in str(l1[i])]
IndexError: string index out of range

And I tried

 b1=[ord(x) for x in str(l1[i])]

but still doesn't work.

btw, I've got a long-time question:I'm always dealing with the line in the linelist (or key in the list), is the line I got from the list, a string? Or need I first convert the line into string first?

thx

edit:bigger chunks of code:

l1= commands.getoutput('zcat '+fastqfile1)
l2= commands.getoutput('zcat '+fastqfile2)
f1=[]
f2=[]
for i in range(0,len(l1)):
    if i % 4 == 3:
       b1=[ord(x) for x in l1[i]]
       ave1=sum(b1)/float(len(l1[i]))
       b2=[ord(x) for x in str(l2[i])]
       ave2=sum(b2)/float(len(l2[i]))
       if (ave1 >= 20 and ave2>= 20):.............


It would be easier to see what's going on with a bigger code chunk. Is this happening in a loop with i as the loop index? In any case, the only indexing happening is in l1[i], so what's happening is your i is greater than the length of l1. Also, make sure l1 is really a list and not a string. string index out of range sounds like it's actually a string.


I'm going to guess that l1 is not a list but a string, based on the error message. l1[i] is the only indexing you're doing in the expression.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号