Home > Mobile >  How do you split full name to first and last names if some people only put their first name in the l
How do you split full name to first and last names if some people only put their first name in the l

Time:07-12

#What I have so far that doesn't work as I get the error: ValueError: not enough values to unpack (expected 2, got 1).

for cell in ws[fullname_column][1:]:
    firstname, lastname = cell.value.split()
    ws.cell(row=cell.row, column=firstname_column).value = firstname
    ws.cell(row=cell.row, column=lastname_column).value = lastname

CodePudding user response:

Instead of

firstname, lastname = cell.value.split()

You should

  1. Split and check the length of the returned list.
  2. If length is 1 then probably you only have 1st name
  3. If length is 2 then u have both 1st and last name

CodePudding user response:

in openpyxl you can get values by rows, something like this:

sheet[f"A{index}"].value)

So if column A is name and B is a surname, you can get values for both individually.

Here index is row number

  • Related