如何读取 python 中的 excel 文件并重写它?

How to read excel file in python and rewrite it?

我有一个Excelsheet全地址,根据这些地址(邮政编码)判断它们是否属于North/South/East/West地区。

地址示例:

164 Penang Road, 01-05, 238464

238463 是邮政编码,North/South/East/West 区域是根据前两个数字'23'来确定的。

这是我当前的代码:

import xlrd

loc = ("C:\Users\Desktop\Windflower\address.xlsx")

wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0, 0)

for i in range(sheet.nrows):
   print(sheet.cell_value(i, 0))

如何编写一个只读取邮政编码的前 2 个数字并确定它属于哪个区域的程序?

所以我的输出将被放入一个新的 excel sheet 中:

我刚开始编码,我读到可以使用 if/else 方法,但到目前为止我遇到了错误。

按照你的代码风格,你可以试试这个

for i in range(sheet.nrows):
    address = sheet.cell_value(i,0)
    address = address.rstrip() #to remove trailing spaces if any
    zip = address[-6:-4]  #to get the first two digits of zip