get data behind " , " to take phrase by python

80 Views Asked by At

I want to get data in Column D behind " , " in the end of the sentence from left to right to get phrase in link bio:

[1]:( http://prntscr.com/fye9hi) "here"

Someone cant help me please ....

This is my code but it cant go like i want.

import xlrd 

file_location = "C:/Users/admin/DataKH.xlsx"
wb = xlrd.open_workbook(file_location)
sheet = wb.sheet_by_index(0)
print(sheet.nrows)
print(sheet.ncols)


for rows in range(sheet.nrows):
    row_0 = sheet.cell_value(rows,0)

from xlwt import Workbook 
import xlwt
from xlwt import Formula


workbook = xlrd.open_workbook(file_location)
sheet = workbook.sheet_by_index(0)



data = [sheet.cell_value(row,3) for row in range(sheet.nrows)]
data1 = [sheet.cell_value(row, 4) for row in range(sheet.nrows)]


workbook = xlwt.Workbook()
sheet = workbook.add_sheet('test')

for index, value in enumerate(data):
    sheet.write(index, 0, value)

for index, value in enumerate(data1): 
    sheet.write(index, 1 , value) 

workbook.save('output.xls')  
1

There are 1 best solutions below

12
On BEST ANSWER

How about using split(",") method? It returns a list of phrases so you can easily iterate through it though.


@MinhTuấnNgô: I'm confused with xlrd's syntax so I switched to pandas instead.

import pandas as pd

df = pd.read_excel('SampleData.xlsx')

df['Extracted Address'] = pd.Series((cell.split(',')[-1] for cell in df['Address']), index = df.index)

Not sure what you mean by 'getting the data after the comma' but this shows a way to manipulate the cell data.

After you've finished formatting the data, you can export it back to excel using df.to_excel(<filepath>)


For xlrd, you can iterate through a specific column using this syntax: for row in ws.col(2)[1:]: This should skip the first row (as taken care of in the case of pandas anyway) and iterate all remaining rows.