我写了一个python脚本来创建一个整数的二进制文件.
import struct
pos = [7623, 3015, 3231, 3829]
inh = open('test.bin', 'wb')
for e in pos:
inh.write(struct.pack('i', e))
inh.close()
Run Code Online (Sandbox Code Playgroud)
它工作得很好,然后我尝试使用下面的代码读取'test.bin'文件.
import struct
inh = open('test.bin', 'rb')
for rec in inh:
pos = struct.unpack('i', rec)
print pos
inh.close()
Run Code Online (Sandbox Code Playgroud)
但它失败并显示错误消息:
Traceback (most recent call last):
File "readbinary.py", line 10, in <module>
pos = struct.unpack('i', rec)
File "/usr/lib/python2.5/struct.py", line 87, in unpack
return o.unpack(s)
struct.error: unpack requires a string argument of length 4
Run Code Online (Sandbox Code Playgroud)
我想知道如何使用这些文件阅读struct.unpack.
非常感谢,Vipin
鉴于该字符串:
\n \n text1\n \ttext2\n Message: 1st message\n some more text\n \n \n Message: 2dn message\n\n \t\t Message: 3rd message\n text3\n
我想从多行字符串中提取消息(令牌是'消息:').我应该使用什么正则表达式来捕获这3组:
我尝试了很多东西,但我可以让表达式工作,因为字符串是一个多行字符串.
我的程序是在python 2.6中,但我认为它使用的语言并没有太大的区别......
请看下面的代码:
import string
from collections import defaultdict
first_complex=open( "residue_a_chain_a_b_backup.txt", "r" )
first_complex_lines=first_complex.readlines()
first_complex_lines=map( string.strip, first_complex_lines )
first_complex.close()
second_complex=open( "residue_a_chain_a_c_backup.txt", "r" )
second_complex_lines=second_complex.readlines()
second_complex_lines=map( string.strip, second_complex_lines )
second_complex.close()
list_1=[]
list_2=[]
for x in first_complex_lines:
if x[0]!="d":
list_1.append( x )
for y in second_complex_lines:
if y[0]!="d":
list_2.append( y )
j=0
list_3=[]
list_4=[]
for a in list_1:
pass
for b in list_2:
pass
if a==b:
list_3.append( a )
kvmap=defaultdict( int )
for k in list_3:
kvmap[k]+=1
print kvmap
Run Code Online (Sandbox Code Playgroud)
通常我使用izip或izip_longest来为两个循环播放,但这次文件的长度是不同的.我不想要一个无条目.如果我使用上述方法,则运行时间变为增量且无用.我怎么能让两个for循环去?
干杯,Chavanak
我们有两个清单:
a=['1','2','3','4']
b=['2','3','4','5']
Run Code Online (Sandbox Code Playgroud)
如何获取包含在两个列表中的元素的列表:
a_and_b=['2','3','4']
Run Code Online (Sandbox Code Playgroud)
以及包含仅包含在一个列表中但不包含在另一个列表中的元素的列表:
only_a=['1']
only_b=['5']
Run Code Online (Sandbox Code Playgroud)
是的,我可以使用周期,但它很蹩脚=)
我有一个包含元组和长整数的列表,列表如下所示:
table = [(1L,), (1L,), (1L,), (2L,), (2L,), (2L,), (3L,), (3L,)]
Run Code Online (Sandbox Code Playgroud)
如何将表转换为正式列表?
所以输出将是:
table = ['1','1','1','2','2','2','3','3']
Run Code Online (Sandbox Code Playgroud)
出于信息目的,数据是从mysql数据库获得的.
如果我有两个字典,我想用Python结合,即
a = {'1': 1, '2': 2}
b = {'3': 3, '4': 4}
Run Code Online (Sandbox Code Playgroud)
如果我对它们运行更新,它会重新排序列表:
a.update(b)
{'1': 1, '3': 3, '2': 2, '4': 4}
Run Code Online (Sandbox Code Playgroud)
当我真正想要的是将"b"附加到"a"的末尾:
{'1': 1, '2': 2, '3': 3, '4': 4}
Run Code Online (Sandbox Code Playgroud)
是否有一种简单的方法将"b"附加到"a"的末尾,而不必像这样手动组合它们:
for key in b:
a[key]=b[key]
Run Code Online (Sandbox Code Playgroud)
像+ =或append()这样的东西是理想的,但当然也不适用于字典.
abc = [0, ] * datalen;
Run Code Online (Sandbox Code Playgroud)
" datalen"是一个Integer.
然后我看到像这样的引用:
abc[-1]
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我想将列表中的字符串括在<>(格式为<%s>).当前代码执行以下操作:
def create_worker (general_logger, general_config):
arguments = ["worker_name", "worker_module", "worker_class"]
__check_arguments(arguments)
def __check_arguments(arguments):
if len(sys.argv) < 2 + len(arguments):
print "Usage: %s delete-project %s" % (__file__," ".join(arguments))
sys.exit(10)
Run Code Online (Sandbox Code Playgroud)
当前输出如下所示:
Usage: ...\handler_scripts.py delete-project worker_name worker_module worker_class
Run Code Online (Sandbox Code Playgroud)
并且应该如下所示:
Usage: ...\handler_scripts.py delete-project <worker_name> <worker_module> <worker_class>
Run Code Online (Sandbox Code Playgroud)
有没有简短的方法来做到这一点?
问候,迈克尔
def merge(l1,l2):
i=0;
while((l1[i]!=none)||(l2[i]!=none)):
Run Code Online (Sandbox Code Playgroud)
SyntaxError:无效语法是一个新手,我无法弄清楚abouve代码有什么问题.