python-如何用法语分析字符串以获取'/'两侧的数字>

问题描述 投票:1回答:1

我正在尝试在python中解析一个包含分数的字符串,在该字符串中,我需要将分数两侧的数字存储为不同的变量。它适用于两面都是一位数字,但是当有两位数字时,我只是得到一个数字的“ /”符号,而只有第二部分的第一个数字。输入文件如下所示:

product/productId: B001EO5QW8
review/userId: AOVROBZ8BNTP7
review/profileName: S. Potter
**review/helpfulness: 19/19**
review/score: 4.0

我的代码如下:

if 'review/helpfulness' in line:
    helpline = line.rstrip().split(': ')[1:] 
    number_voted = helpline[0][0]
    number_rated = helpline[0][2]

我得到这个答案:

NumHelpfulVotes         NumVotes
   1                       /

我正在尝试在python中解析一个包含分数的字符串,在该字符串中,我需要将分数两侧的数字存储为不同的变量。它适用于两侧的单个数字,但...

python python-3.x split
1个回答
1
投票

一种解决方案是用斜杠进一步分割:

© www.soinside.com 2019 - 2024. All rights reserved.