-- coding: utf-8 -- import xlrd import xlwt Open the existing Excel file workbook =…
- User
-- coding: utf-8 --
import xlrd import xlwt
Open the existing Excel file
workbook = xlrd.open_workbook('normal.xlsx') worksheet = workbook.sheet_by_index(0)
Create a new excel workbook
output = xlwt.Workbook(encoding = 'utf8') new_sheet = output.add_sheet('My Worksheet')
Iterate through each row of column A and B
for i in range(1, worksheet.nrows):
Get the values in each column
col_a = worksheet.cell_value(i, 0) col_b = worksheet.cell_value(i, 1)Look for matches in other cells in the same row
for j in range(i+1, worksheet.nrows): if col_a == worksheet.cell_value(j, 2) or col_b == worksheet.cell_value(j, 2): new_sheet.write(i, 2, worksheet.cell_value(j, 2))
Save the result in a new Excel file
output.save('Excel_Workbook_sele.xls')
- Assistant
I think there might be a mistake in the code, let me check
-- coding: utf-8 --
import xlrd import xlwt
Open the existing Excel file
workbook = xlrd.open_workbook('normal.xlsx') worksheet = workbook.sheet_by_index(0)
Create a new excel workbook
output = xlwt.Workbook(encoding = 'utf8') new_sheet = output.add_sheet('My Worksheet')
Iterate through each row of column A and B
for i in range(1, worksheet.nrows): # Get the values in each column col_a = worksheet.cell_value(i, 0) col_b = worksheet.cell_value(i, 1)
# Look for matches in other cells in the same row for j in range(i+1, worksheet.nrows): if col_a == worksheet.cell_value(j, 0) or col_b == worksheet.cell_value(j, 1): new_sheet.write(i, 2, worksheet.cell_value(j, 2))Save the result in a new Excel file
output.save('Excel_Workbook_sele.xls')
- User
new_sheet.write(i, 2, worksheet.cell_value(j, 2)) IndentationError:expected an indented blic
- Assistant
You are right, the indentation is missing before the line
new_sheet.write(i, 2, worksheet.cell_value(j, 2)).Here's the corrected code with proper indentation:
-- coding: utf-8 --
import xlrd import xlwt
Open the existing Excel file
workbook = xlrd.open_workbook('normal.xlsx') worksheet = workbook.sheet_by_index(0)
Create a new excel workbook
output = xlwt.Workbook(encoding = 'utf8') new_sheet = output.add_sheet('My Worksheet')
Iterate through each row of column A and B
for i in range(1, worksheet.nrows): # Get the values in each column col_a = worksheet.cell_value(i, 0) col_b = worksheet.cell_value(i, 1)
# Look for matches in other cells in the same row for j in range(i+1, worksheet.nrows): if col_a == worksheet.cell_value(j, 0) or col_b == worksheet.cell_value(j, 1): new_sheet.write(i, 2, worksheet.cell_value(j, 2))Save the result in a new Excel file
output.save('Excel_Workbook_sele.xls')
- User
permissiponError: [errno 13] permission denied:'excel_workbook_sele.xls'
- Assistant
The error message indicates that you don't have permission to write to the Excel file. This could happen if the file is being used by another program or if you don't have the necessary privileges to write to the file.
To fix this, you can try closing any other program that may be using the file and then run the script again. You can also try running the script with administrative privileges, or save the file to a different location where you have the necessary permissions.
In addition, make sure that the file is not read-only. You can check this by right-clicking on the file, selecting Properties, and then make sure the Read-only box is not checked.
- User
indexError: list index out of range
- Assistant
The error message "IndexError: list index out of range" means that you are trying to access an index that is outside the range of the list. This could happen when trying to access an element at an index that doesn't exist.
In the code you provided, it's likely that the error is occurring in the line
col_b = worksheet.cell_value(i, 1). Check that all rows in your worksheet have at least two columns of data and that there are no empty cells or additional columns with data.You can also print out the values of
iandworksheet.nrowsto debug and ensure that you are iterating over the correct range of rows.Alternatively, you can try using exception handling to catch the error and print out more information about the error, such as the index that is out of range.
Here's an example of how to use exception handling to catch the IndexError:
for i in range(1, worksheet.nrows): try: col_a = worksheet.cell_value(i, 0) col_b = worksheet.cell_value(i, 1) except IndexError as e: print(f"IndexError: {e} at row {i}")This code will print out the row number and the specific index that caused the error, which can help you diagnose the issue.
- User
Look for matches in other cells in the same row
for j in range(i+1, worksheet.nrows): if col_a == worksheet.cell_value(j, 1) or col_b == worksheet.cell_value(j, 0): new_sheet.write(i, 2, worksheet.cell_value(j, 2))
Find out if the contents of all cells in column B exist in column A
- Assistant
To find out if the contents of all cells in column B exist in column A, you can try something like this:
# Open the existing Excel file workbook = xlrd.open_workbook('normal.xlsx') worksheet = workbook.sheet_by_index(0) # Create a new excel workbook output = xlwt.Workbook(encoding='utf8') new_sheet = output.add_sheet('My Worksheet') # Iterate through each row to check if the contents of column B exist in column A for i in range(1, worksheet.nrows): # Get the value in column B col_b = worksheet.cell_value(i, 1) # Check if the value exists in column A if col_b in worksheet.col_values(0): new_sheet.write(i, 2, "Yes") else: new_sheet.write(i, 2, "No") # Save the result in a new Excel file output.save('Excel_Workbook_sele.xls')This code iterates through each row of the Excel file, gets the value in column B, and checks if it exists in column A by using the
inkeyword to check if the value exists in the list of column A values (worksheet.col_values(0)). If the value exists in column A, it writes "Yes" in a new column, otherwise it writes "No". The results are then saved in a new Excel file.