将元组拆分为不同的文件

时间:2021-01-24 07:33:53

I have a list of URL's that I would like to split up on a tuple with certain chars, such as 'p' or 'blog' (the full tuple looks like this: [('p', '958')][('blog', '3')][('p', '2512')]). I would like to split and write these into separate, appropriately named files(p_file.txt, blog_file.txt, etc). I, however, am stuck. I have been trying multiple ways but I have not been having any success. Would appreciate any help.

我有一个URL列表,我希望将其拆分为带有某些字符的元组,例如'p'或'blog'(完整的元组看起来像这样:[('p','958')] [( '博客','3')] [('p','2512')])。我想将它们拆分并写入单独的,适当命名的文件(p_file.txt,blog_file.txt等)。然而,我被卡住了。我一直在尝试多种方式,但我没有取得任何成功。非常感谢任何帮助。

def parse_file():
    infile = open("URLlist.txt", 'r')
    outfile = open("newfile4.txt", 'w')
    lines = infile.readlines()
    for line in lines:
        line = line.strip()
        url = parse_qsl(urlparse(line)[4])
        if url:
            print url
parse_file()

1 个解决方案

#1


0  

To get all 'p' tuples in the list, just iterate over to find the match. List comprehensions make this easy.

要获取列表中的所有'p'元组,只需迭代以找到匹配项。列表理解使这很容易。

urls = ( [('p', '958')],[('blog', '3')],[('p', '2512')])
p_names = [ p_tuple  for url in urls for p_tuple in url if 'p' in p_tuple ]

p_name is now a list with 'p' tuples.

p_name现在是一个带有'p'元组的列表。

#1


0  

To get all 'p' tuples in the list, just iterate over to find the match. List comprehensions make this easy.

要获取列表中的所有'p'元组,只需迭代以找到匹配项。列表理解使这很容易。

urls = ( [('p', '958')],[('blog', '3')],[('p', '2512')])
p_names = [ p_tuple  for url in urls for p_tuple in url if 'p' in p_tuple ]

p_name is now a list with 'p' tuples.

p_name现在是一个带有'p'元组的列表。