无法使用python拆分数据

jbose2ul  于 2022-12-05  发布在  Python
关注(0)|答案(6)|浏览(151)

I have a data like below:

data = """1000
2000
3000

4000

5000
6000

7000
8000
9000

10000"""

Now, I want to sum up the elements that appear before the space and maintain the max_sum track with the sum of the next elements that appear before the empty line. So for me, it should be the sum of 1000,2000,3000 = 6000 compared with the initial max_sum for eg 0 , and now sum the next element i.e 4000 , and keep comparing with the max_sum which could be like max(6000, 4000) = 6000 and keep on doing the same but need to reset the sum if I encounter a empty line.
Below is my code:

max_num = 0
    sum = 0
    for line in data:
        # print(line)
        sum = sum + int(line)
        if line in ['\n', '\r\n']:
            sum=0
        max_num = max(max_num, sum)

This gives an error:

sum = sum + int(line)
ValueError: invalid literal for int() with base 10: '\n'
zqry0prt

zqry0prt1#

您正在尝试将空行转换为int:

max_num = 0
sum = 0
for line in data:
    print(line)
    if line.strip():
        sum = sum + int(line)
    if line in ['\n', '\r\n']:
        sum=0
    max_num = max(max_num, sum)
hrysbysz

hrysbysz2#

这里有一个简短的说明:

data = """1000
2000
3000

4000

5000
6000

7000
8000
9000

10000"""

max(
    sum(
        int(i) for i in l.split('\n')
    ) for l in data.split('\n\n')
)

得到24000
首先,它基于\n\n进行划分,然后基于\n进行划分。对组中的所有元素求和,然后选择最大值。

avkwfej4

avkwfej43#

请注意,int() 不受前导空格和尾随空格的影响-例如,int('\n99\n') 将得到99而不会出错。但是,完全由空格组成的字符串将导致ValueError。这就是这里发生的情况。您正在尝试解析一个只包含换行符的字符串。
您可以对这些数据利用ValueError,如下所示:

data = """1000
2000
3000

4000

5000
6000

7000
8000
9000

10000"""

current_sum = 0
max_sum = float('-inf')

for t in data.splitlines():
    try:
        x = int(t)
        current_sum += x
    except ValueError:
        max_sum = max(max_sum, current_sum)
        current_sum = 0

print(f'Max sum = {max(max_sum, current_sum)}')

输出:

Max sum = 24000
zf9nrax1

zf9nrax14#

有些行仅由“\n”组成,而您正尝试将其转换为int。如果该行是“\n”或“\r\n”,则应将行的测试移到int转换和continue之前,而不转换为int

qoefvg9y

qoefvg9y5#

不要使用sum这样的内置名称,这里你需要将数据拆分到\n中,你会得到list,然后你可以循环使用strip()删除空格,如果line有一些数字,它会对它求和,否则它会赋值0。

max_num = 0
sum_val = 0

for line in data.split("\n"):
    line = line.strip()
    sum_val = int(line) + sum_val if line and line.isdigit() else 0
    max_num = max(max_num, sum_val)
print(max_num)
yhuiod9q

yhuiod9q6#

您可以尝试:

data = """1000
    2000
    3000
    
    4000
    
    5000
    6000
    
    7000
    8000
    9000
    
    10000
    """

data = data.splitlines()

max_sum = 0
group = []

for data_index, single_data in enumerate(data):
    single_data = single_data.replace(" ","")
    if single_data == "":
        if max_sum < sum(group):
            max_sum = sum(group)
        group = []
    else:
        group.append(int(single_data))

print(max_sum)

输出量:

24000

相关问题