Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从列表中对类别中的值进行排序,以便从python中的变量访问数据?_Python_List_Sorting_If Statement - Fatal编程技术网

如何从列表中对类别中的值进行排序,以便从python中的变量访问数据?

如何从列表中对类别中的值进行排序,以便从python中的变量访问数据?,python,list,sorting,if-statement,Python,List,Sorting,If Statement,我有一个python scraping脚本来获取一些即将举行的音乐会的信息,每次都是相同的文本模式,无论有多少场音乐会出现,唯一的区别是,有时它会在门票仍然可以预订时显示一行额外的价格,例如下面的示例: LIVE 01/01/99 9PM Iron Maiden Madison Square Garden New York City LIVE 01/01/99 9.30PM The Doors Staples Center Los Angeles LIVE 01/02/99 8.45PM Dr

我有一个python scraping脚本来获取一些即将举行的音乐会的信息,每次都是相同的文本模式,无论有多少场音乐会出现,唯一的区别是,有时它会在门票仍然可以预订时显示一行额外的价格,例如下面的示例:

LIVE 01/01/99 9PM
Iron Maiden
Madison Square Garden 
New York City
LIVE 01/01/99 9.30PM
The Doors
Staples Center
Los Angeles
LIVE 01/02/99 8.45PM
Dr Dre & Snoop Dogg
Staples Center
Los Angeles
Book a ticket now for $99,99
LIVE 01/02/99 9PM
Diana Ross
City Hall
New York City 
Book a ticket now for $79,99       ect...
我需要在两个类别(4行块和5行块)之间对这些块进行排序,然后遍历变量中的值(带、日期、位置、价格等)

如果我只有4行块或5行块,它工作得非常好,但是当我有两种块时,就像在我的示例文本中一样,我不知道如何将它们放入它们自己的类别中,我在我的if语句中尝试了许多公式,但都不起作用

live_lines = []
line_counter = 0
distances = []
with open('concerts_list.txt', 'r') as file:
    reading_file = file.read()
    lines = reading_file.split('\n')
    for line in lines:
        if line.startswith('LIVE'):
            live_lines.append(line_counter)

        line_counter += 1

for position in range(len(live_lines)-1):
    block_lines = live_lines[position+1] - live_lines[position]
    block_sizes.append(block_lines)

print('live_lines:', live_lines) #output = [0,4,8,13]
print('block_sizes', block_sizes) #output = [4,4,5]

if block_sizes == 4 for block_lines in live_lines:
    dates = [i for i in lines [0::4]] #output desired = ["LIVE 01/01/99 9PM", "LIVE 01/01/99 9.30PM", "LIVE 01/02/99 8.45PM"]
if block_sizes == 5 for block_lines in live_lines:
    dates = [i for i in lines [0::5]] #output desired = ["LIVE 01/02/99 9PM"]
当只有4行代码块时,这个
日期
变量行代码在没有if语句的情况下工作得非常好,但当出现5行代码块时,它会变得混乱,读取的字符数会减少1个字符

if block_sizes == 4 for block_lines in live_lines:
    dates = [i for i in lines [0::4]] 
elif block_sizes == 4 for block_lines in live_lines:
    dates = [i for i in lines [0::5]] 
当只有5行代码块时,这个
日期
变量行代码在没有if语句的情况下工作得非常好,但当出现4行代码块时,它会变得混乱并读取1个字符

if block_sizes == 4 for block_lines in live_lines:
    dates = [i for i in lines [0::4]] 
elif block_sizes == 4 for block_lines in live_lines:
    dates = [i for i in lines [0::5]] 

我建议使用正则表达式来解析此文本。例如,此正则表达式字符串将所有行分组为单独的组:

((LIVE.*)\n(.*)\n(.*)\n(.*)\n(Book.*)|(LIVE.*)\n(.*)\n(.*)\n(.*))
例如,如果变量文本包含示例文本:

import re

with open("concerts_list.txt", "r") as r:
    text = r.read()

    pattern = r"((LIVE.*)\n(.*)\n(.*)\n(.*)\n(Book.*)|(LIVE.*)\n(.*)\n(.*)\n(.*))"
    matches = re.findall(pattern, text, re.MULTILINE)
    matches = [[line for line in match if line] for match in matches] # filter empty groups

    for match in matches:
        if len(match) == 5: #no price data
            print(match[1])
        else:   # price data
            print(match[1])

我建议使用正则表达式来解析此文本。例如,此正则表达式字符串将所有行分组为单独的组:

((LIVE.*)\n(.*)\n(.*)\n(.*)\n(Book.*)|(LIVE.*)\n(.*)\n(.*)\n(.*))
例如,如果变量文本包含示例文本:

import re

with open("concerts_list.txt", "r") as r:
    text = r.read()

    pattern = r"((LIVE.*)\n(.*)\n(.*)\n(.*)\n(Book.*)|(LIVE.*)\n(.*)\n(.*)\n(.*))"
    matches = re.findall(pattern, text, re.MULTILINE)
    matches = [[line for line in match if line] for match in matches] # filter empty groups

    for match in matches:
        if len(match) == 5: #no price data
            print(match[1])
        else:   # price data
            print(match[1])

哪里是
如果块大小==4
相对于完整的代码?就在两行打印之后,我编辑了我的代码以包含它,哪里是
如果块大小==4
相对于完整的代码?就在两行打印之后,我编辑了我的代码以包含它,但我需要保持顺序,逻辑是在文本文件中的每个块之间迭代,查看它包含多少行(4行或5行),在2个类别(4行或5行块)之间对块进行排序然后根据块所属的类别访问变量中的数据这是我在注释中编辑的代码不可能的?我不知道正则表达式中的任何内容,我也从未使用过我的if语句,所以我不熟悉,我宁愿保持相同的逻辑,并尝试为我的if语句获得正确的公式,因为其他一切都在我的代码中工作,我不想弄乱任何东西。这真的没那么难,只要用
print(match[1])
替换
pass
,你就可以打印所有音乐会的日期。你说的match是什么意思?那么[1]呢?以及#无价格日期为5(价格数据为5)?因此,请使用与我相同的变量来增加清晰度,因为我确实得到了你的正则表达式部分,但我完全迷失了。谢谢谢谢谢谢,但我需要保持顺序,逻辑是在文本文件中的每个块之间迭代,看它包含多少行(4或5行),在两个类别(4或5行块)之间对块进行排序然后根据块所属的类别访问变量中的数据这是我在注释中编辑的代码不可能的?我不知道正则表达式中的任何内容,我也从未使用过我的if语句,所以我不熟悉,我宁愿保持相同的逻辑,并尝试为我的if语句获得正确的公式,因为其他一切都在我的代码中工作,我不想弄乱任何东西。这真的没那么难,只要用
print(match[1])
替换
pass
,你就可以打印所有音乐会的日期。你说的match是什么意思?那么[1]呢?以及#无价格日期为5(价格数据为5)?所以,请使用与我相同的变量来增加清晰度,因为我确实得到了你的正则表达式部分,但我后来完全迷路了,谢谢