开发者

Python: Setting conditions within if-elif-else block

I have the following code snippet:

for row in lst:
    if 'Type' in row[0]:
        for col in range(len(row)):
  开发者_JAVA技巧          #do something
    elif (not 'Type' or '') in row[0]:
        for col in range(len(row)):
            #do something
row_count +=1

For the second part of elif (not 'Type' or '') how do I NOT increase the row_count counter if its true for row[0] = '' only but increase when row[0] != Type is met ? I coudnt set the counter condition withion the if-else block as im scanning row by row in lst which is basically a row read from a csv reader in python.

Perhaps I'm complicating myself too much? Advices plz.

[EDIT] - Here's the actual code, im using xlwt module to write into worksheets.

for row in spamReader:
    if 'Type' in row[0]:
        for col in range(len(row)):
            ws.write(0,col,convert(row[col]),style) 
    elif (not 'Type' or '') in row[0]:
        for col in range(len(row)):
            ws.write(row_count,col,convert(row[col]),style)
    row_count +=1

More details: I have this csv file where Im scanning row by row. Whichever row where its 1st value is 'Type', im writing it in another xls worksheet so that it becomes top row. Else, it just continues to copy row by row. But, when the row's 1st column is empty, i.e. '', it suppose to pass/neglect it.


How about this:

for row in spamReader:
    if row[0] == '':
        continue

    if 'Type' in row[0]:
        row_count = 0
        ... start new worksheet ...

    for col in range(len(row)):
        ws.write(row_count,col,convert(row[col]),style) 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜