简体   繁体   English

将字符串转换为ASCII代码

[英]convert string into ASCII code

I need to convert string into ASCII code. 我需要将字符串转换为ASCII代码。 I'm using python. 我正在使用python。 I did as below: 我做了如下:

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

here l1 is a linelist, l1[i] is the ith line of l1 这里l1是一个线表,l1 [i]是l1的第i行

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):.............

I'm going to guess that l1 is not a list but a string, based on the error message. 我将基于错误消息猜测l1不是列表而是字符串。 l1[i] is the only indexing you're doing in the expression. l1[i]是您在表达式中唯一的索引。

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? 这是在以i作为循环索引的循环中发生的吗? 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. 无论如何,唯一的索引发生在l1 [i]中,所以发生的是您的i大于l1的长度。 Also, make sure l1 is really a list and not a string. 另外,请确保l1实际上是一个列表,而不是字符串。 string index out of range sounds like it's actually a string. string index out of range听起来像它实际上是一个字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM