将某个字符之前的数字变成一个单独的对象

Turn digits before a certain character into a separate object

我有一个定义为一些数字的对象,一个由 space 包围的正斜杠,然后还有一些数字,我想将正斜杠之前的数字变成一个单独的对象。以下是一些示例:

4026 / 1769395
5160 / 1769395
5158 / 1769395

示例是我用这段代码得到的HTML table中不同单元格的内容('curver'和'curcol'是定义诗句和正在使用的单元格的列):

cellcontent = driver.find_element_by_xpath('/html/body/div/table[8]/tbody/tr[' + str(curver - 3) +']/td[' + str(curcol + 1) + ']').text

print(cellcontent)

我知道看起来更简单的方法是只包含前四位数字,或者包含除最后 10 位数字之外的所有内容,但示例看起来像这样,因为我正在使用数据集测试代码.有时第一个或第二个数字可以有更少或更多的数字。

如何只包含 space 和正斜杠前的数字?

简单如:

string = '4026 / 1769395'
#Remove the int if you dont actually need them as numbers but as string.
first_digits = int(string.split(' / ')[0])

此外,如果您正在使用数据帧:

import pandas as pd
df = pd.DataFrame([['4026 / 1769395'],
                   ['5160 / 1769395'],
                   ['5158 / 1769395']], columns=['column_1'])

df['first_digits'] = df.column_1.apply(lambda x: x.split(' / ')[0])

df['second_digits'] = df.column_1.apply(lambda x: x.split(' / ')[1])

df

输出:

      column_1    first_digits  second_digits
0   4026 / 1769395   4026      1769395
1   5160 / 1769395   5160      1769395
2   5158 / 1769395   5158      1769395