如何在excelpython列中写入多行?
How to write multiple rows in column excel python?
我有 'column A' 包含酒店名称,我想在 excel
中为每个位于 'column B' 的酒店写 'loc.address'
例如:
我使用这个代码:
import pandas as pd
from geopy.geocoders import Nominatim
import xlrd
# Give the location of the file
loc = "C:/Users/UI UX/Desktop/test.xlsx"
# To open Workbook
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0, 0)
for i in range(sheet.nrows):
hotel_column = (sheet.cell_value(i, 0))
geolocator = Nominatim(user_agent="abd")
loc = geolocator.geocode(hotel_column, country_codes='', language='')
if loc is None:
print('Cant find latitude & longitude of this place :(')
else:
print("latitude is :", loc.latitude, "\nlongitude is:", loc.longitude)
print('Location Address: ' + loc.address)
print('---------------------------------------------------')
openpyxl
可以更新 xlsx 文件
import pandas as pd
from geopy.geocoders import Nominatim
import xlrd
import openpyxl
# To open Workbook
loc = "C:/Users/UI UX/Desktop/test.xlsx"
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0, 0)
locs=[]
for i in range(sheet.nrows):
hotel_column = (sheet.cell_value(i, 0))
geolocator = Nominatim(user_agent="abd")
loc = geolocator.geocode(hotel_column, country_codes='', language='')
if loc is None:
print('Cant find latitude & longitude of this place :(')
locs.append('Cant find latitude & longitude of this place :(')
else:
print("latitude is :", loc.latitude, "\nlongitude is:", loc.longitude)
print('Location Address: ' + loc.address)
print('---------------------------------------------------')
locs.append('Location Address: ' + loc.address)
xfile = openpyxl.load_workbook("C:/Users/UI UX/Desktop/test.xlsx")
sheet = xfile.worksheets[0]
for i,val in enumerate(locs):
sheet.cell(row=i+1, column=2).value = val
xfile.save('C:/Users/UI UX/Desktop/test.xlsx')
我有 'column A' 包含酒店名称,我想在 excel
中为每个位于 'column B' 的酒店写 'loc.address'例如:
我使用这个代码:
import pandas as pd
from geopy.geocoders import Nominatim
import xlrd
# Give the location of the file
loc = "C:/Users/UI UX/Desktop/test.xlsx"
# To open Workbook
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0, 0)
for i in range(sheet.nrows):
hotel_column = (sheet.cell_value(i, 0))
geolocator = Nominatim(user_agent="abd")
loc = geolocator.geocode(hotel_column, country_codes='', language='')
if loc is None:
print('Cant find latitude & longitude of this place :(')
else:
print("latitude is :", loc.latitude, "\nlongitude is:", loc.longitude)
print('Location Address: ' + loc.address)
print('---------------------------------------------------')
openpyxl
可以更新 xlsx 文件
import pandas as pd
from geopy.geocoders import Nominatim
import xlrd
import openpyxl
# To open Workbook
loc = "C:/Users/UI UX/Desktop/test.xlsx"
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0, 0)
locs=[]
for i in range(sheet.nrows):
hotel_column = (sheet.cell_value(i, 0))
geolocator = Nominatim(user_agent="abd")
loc = geolocator.geocode(hotel_column, country_codes='', language='')
if loc is None:
print('Cant find latitude & longitude of this place :(')
locs.append('Cant find latitude & longitude of this place :(')
else:
print("latitude is :", loc.latitude, "\nlongitude is:", loc.longitude)
print('Location Address: ' + loc.address)
print('---------------------------------------------------')
locs.append('Location Address: ' + loc.address)
xfile = openpyxl.load_workbook("C:/Users/UI UX/Desktop/test.xlsx")
sheet = xfile.worksheets[0]
for i,val in enumerate(locs):
sheet.cell(row=i+1, column=2).value = val
xfile.save('C:/Users/UI UX/Desktop/test.xlsx')