简体   繁体   中英

xlsxwriter OUTPUT formatting

I have a if else statement. What I'm trying to do is.

I'm looping through and seeing if there's a specific text on something if it does; add that text in my output in a specific column if it doesn't leave that field null. However it seems to leave all the field null.

This is my code

for items in parseBio1:

        if "Snapchat" in items:
            worksheet.write_string(row+1,col+13,items)
        else:
            worksheet.write_string(row,col+13,"None")

        if "com" in items:
            worksheet.write_string(row+1, col+11, items)
        else:
            worksheet.write_string(row,col+11,"None")

This is the output I get.

在此处输入图片说明

Per requested. Here's the way I'm parsing my input

parseBio1 = parseBio.split('\n')
    row+=1

    for items in parseBio1:

        if "Snapchat" in items:
            worksheet.write_string(row+1,col+13,items)
        else:
            worksheet.write_string(row,col+13,"None")

This is what happens when i get rid of else statement like this:

for items in parseBio1:

        digit = items.isdigit()


        if "Snapchat" in items:
            worksheet.write_string(row+1,col+13,items)

        if "com" in items:
            worksheet.write_string(row+1, col+11, items)

The output looks messed up and isn't located where it is suppose to be. In this case. Row 0 item 'snapchat' field is in row 1 for a different user like this 在此处输入图片说明

Here is what I would do:

for items in parseBio1:

        if "Snapchat" in items:
            worksheet.write_string(row, col+13, items)
        if "com" in items:
            worksheet.write_string(row, col+11, items)

        row += 1

In this way if either Snapchat or com in items, it will populate the respective columns.

If I'm understanding this correctly, you are iterating through a list of strings that were delimited by '\\n'. If so your conditions for your if statements may be slightly inaccurate. You might be trying to look for words written in different cases, which python does not account for:

>> x = "Testing the waters"
>> "test" in x
False

Therefore, I recommend you calling .lower() on items in your conditionals, and comparing them to an all-lowercase word.

for items in parseBio1:

    # Try to call .lower() on items and compare to an all lowercase string
    if "snapchat" in items.lower():
        worksheet.write_string(row+1,col+13,items)
    else:
        worksheet.write_string(row,col+13,"None")

    if "com" in items.lower():
        worksheet.write_string(row+1, col+11, items)
    else:
        worksheet.write_string(row,col+11,"None")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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