Execution of endswith function

Rach

It seems like if statement doesn't execute or may be because i have made some mistake.

I tried,

ends = line.endswith('/')

if (line.startswith('  ') and ends == True)

But doesn't work. If statement doesn't run

count = 0
for line in f1:
    if (line.startswith('  ') and line.endswith('/')):
        count = count + 1
        continue

    elif line.startswith(('  ', ' ', '//')):
        continue
    else:
        if (count == 0):
            f2.write(line)
            count = 0

If line starts with '//' or single or double spaces, those lines should not be printed (condition 1). Also, if a line starts with double space and ends with '/' and next line doesn't satisfy condition 1, it should no be printed. Lines without condition 1 must be printed

Input:

//abcd

  abcd/

This line should not be printed

This has to be printed

Expected Output:

This has to be printed

Actual output:

This line should not be printed

This has to be printed
blhsing

Lines generated by iterating through a file object always end with a newline character (unless it's the last line of the file and the file does not end with a trailing newline character). You can apply rstrip to the line before you use endswith to test if a line ends with a certain character. Also, you should reset counter (with count = 0) outside of the if (count == 0): block; otherwise the statement would never run:

from io import StringIO
f1 = StringIO('''//abcd
  abcd/
This line should not be printed
This has to be printed
''')
f2 = StringIO()
count = 0
for line in f1:
    if (line.startswith('  ') and line.rstrip().endswith('/')):
        count = count + 1
    elif not line.startswith(('  ', ' ', '//')):
        if (count == 0):
            f2.write(line)
        count = 0
print(f2.getvalue())

This outputs:

This has to be printed

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to return which element from the tuple matched in the endswith function

分類Dev

Python endswith() function cannot avoid copying .py file

分類Dev

Python decorator function execution

分類Dev

Python function execution order

分類Dev

Function Calling Execution in nodejs

分類Dev

Conditional member function execution

分類Dev

PySide Application with asynchronous function execution

分類Dev

Code execution not happening in asyn function

分類Dev

Stop Javascript Function execution from another Function

分類Dev

Promise executes then function before previous then execution is completed

分類Dev

how to stop java script Function execution

分類Dev

How to change the order of function body execution?

分類Dev

How to approximate execution time of ArangoDB count function

分類Dev

Heredocument indented inside a function fails on execution

分類Dev

JavaScriptのendsWith

分類Dev

Django: `__endswith`の逆

分類Dev

Python 3.4はendswith()

分類Dev

execution out of order inside ajax callback function javascript

分類Dev

How can I stop the execution of a Python function from outside of it?

分類Dev

why ng-repeat changes order of link function execution

分類Dev

calculate execution times of async function over multiple calls

分類Dev

what function in Linux API implements execution of a script file with a shebang?

分類Dev

Optimized c++ function for nth prime number execution time

分類Dev

ast execution produces different result in a function vs module scope

分類Dev

With recursive functions in Go, if the inner function returns, does the outer function continue execution normally?

分類Dev

Azure TableStorageの `endsWith`と` startsWith`

分類Dev

C# String EndsWith Returning True Issue

分類Dev

R equivalent of endsWith to check against a list of strings

分類Dev

Differences between using python endswith and substring

Related 関連記事

  1. 1

    How to return which element from the tuple matched in the endswith function

  2. 2

    Python endswith() function cannot avoid copying .py file

  3. 3

    Python decorator function execution

  4. 4

    Python function execution order

  5. 5

    Function Calling Execution in nodejs

  6. 6

    Conditional member function execution

  7. 7

    PySide Application with asynchronous function execution

  8. 8

    Code execution not happening in asyn function

  9. 9

    Stop Javascript Function execution from another Function

  10. 10

    Promise executes then function before previous then execution is completed

  11. 11

    how to stop java script Function execution

  12. 12

    How to change the order of function body execution?

  13. 13

    How to approximate execution time of ArangoDB count function

  14. 14

    Heredocument indented inside a function fails on execution

  15. 15

    JavaScriptのendsWith

  16. 16

    Django: `__endswith`の逆

  17. 17

    Python 3.4はendswith()

  18. 18

    execution out of order inside ajax callback function javascript

  19. 19

    How can I stop the execution of a Python function from outside of it?

  20. 20

    why ng-repeat changes order of link function execution

  21. 21

    calculate execution times of async function over multiple calls

  22. 22

    what function in Linux API implements execution of a script file with a shebang?

  23. 23

    Optimized c++ function for nth prime number execution time

  24. 24

    ast execution produces different result in a function vs module scope

  25. 25

    With recursive functions in Go, if the inner function returns, does the outer function continue execution normally?

  26. 26

    Azure TableStorageの `endsWith`と` startsWith`

  27. 27

    C# String EndsWith Returning True Issue

  28. 28

    R equivalent of endsWith to check against a list of strings

  29. 29

    Differences between using python endswith and substring

ホットタグ

アーカイブ