如何在python中将charcter与新行进行比较

时间:2018-05-29 16:09:30

标签: python

如何在python中将char与'\ n'进行比较?这是我正在尝试做的事情:

def grabInstructionsExecuted (str, beg):
    index = beg
    while str[index] != '\n'
        index += 1;
    return str[beg:index-1]

然而,我收到此错误:

File "grabMIPS.py", line 7
    while str[index] != '\n'
                           ^
SyntaxError: invalid syntax

我是python的新手,这看起来非常基本,但我无法让它工作。谢谢。

2 个答案:

答案 0 :(得分:3)

你在以后忘记了结肠:

def grabInstructionsExecuted (str, beg):
    index = beg
    while str[index] != '\n':  # you need a colon here 
        index += 1;
    return str[beg:index-1]

答案 1 :(得分:1)

逐字逐句地遍历字符串是缓慢且不必要的。

## Get graphs for each community
C1 = induced_subgraph(g, which(membership(CL) == 1))
C2 = induced_subgraph(g, which(membership(CL) == 2))

plot(C1)
plot(C2)