Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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 CSV如果行包含字符串附加行_Python_Csv - Fatal编程技术网

Python CSV如果行包含字符串附加行

Python CSV如果行包含字符串附加行,python,csv,Python,Csv,如果一行包含mobilesitemap-browse.csv中的字符串,我尝试将sitemap_bp.csv中的一行附加到相邻列中。 我无法遍历mobilesitemap-browse.csv中的行,它被卡在第一行。我该如何着手解决这个问题 import csv with open('sitemap_bp.csv','r') as csvinput: with open('mobilesitemap-browse.csv','r') as csvinput2: with

如果一行包含mobilesitemap-browse.csv中的字符串,我尝试将sitemap_bp.csv中的一行附加到相邻列中。 我无法遍历mobilesitemap-browse.csv中的行,它被卡在第一行。我该如何着手解决这个问题

import csv

with open('sitemap_bp.csv','r') as csvinput:
    with open('mobilesitemap-browse.csv','r') as csvinput2:
        with open('output.csv', 'w') as csvoutput:
            writer = csv.writer(csvoutput, lineterminator='\n')
            sitemap = csv.reader(csvinput)
            mobilesitemap = csv.reader(csvinput2)

            all = []
            row = next(sitemap)
            row.append('mobile')
            all.append(row)

            for mobilerow in mobilesitemap:
                for row in sitemap:
                    #print row[0]
                    if mobilerow[1] in row[0]:
                        #print row, mobilerow[1]
                        all.append((row[0], mobilerow[1]))
                    else:
                        all.append(row)

            writer.writerows(all)

就我个人而言,我会先解析sitemap_bp.csv中的数据,然后使用该字典填充新文件

import re

with open('sitemap_bp.csv','r') as csvinput, \
        open('mobilesitemap-browse.csv','r') as csvinput2, \
        open('output.csv', 'w') as csvoutput:
    writer = csv.writer(csvoutput, lineterminator='\n')
    sitemap = csvinput # no reason to pipe this through csv.reader
    mobilesitemap = csv.reader(csvinput2)

    item_number = re.compile(r"\d{5}_\d{7}_{7}")

    item_number_mapping = {item_number.search(line).group(): line.strip()
                           for line in sitemap if item_number.search(line)}
    # makes a dictionary {item_number: full_url, ...} for each item in sitemap
    # alternate to the above, consider:
    # # item_number_mapping = {}
    # # for line in sitemap:
    # #     line = line.strip()
    # #     match = item_number.search(line)
    # #     if match:
    # #         item_number_mapping[match.group()] = match.string

    all = [row + [item_number_mapping[row[1]] for row in mobilesitemap]

    writer.writerows(all)
我的猜测是,在第一次通过外部
for
循环后,它会尝试再次迭代
站点地图,但无法,因为文件已经用完了。这方面的最小变化是:

        for mobilerow in mobilesitemap:
            csvinput.seek(0) # seek to the start of the file object
            next(sitemap) # skip the header row
            for row in sitemap:
                #print row[0]
                if mobilerow[1] in row[0]:
                    #print row, mobilerow[1]
                    all.append((row[0], mobilerow[1]))
                else:
                    all.append(row)
但不这样做的明显原因是,它在
mobilesitemap browse.csv
中每行迭代一次您的
sitemap\u bp.csv
文件,而不是像我的代码那样只迭代一次

编辑评论中的每个问题 如果您需要在
sitemap\u bp.csv
中获取与
mobilesitemap browse.csv
不对应的URL列表,最好为您看到的所有项目设置一个
set
,然后使用set操作获取未看到的项目。这需要一些修补,但是

# instead of all = [row + [item number ...

seen = set()
all = []

for row in mobilesitemap:
    item_no = row[1]
    if item_no in item_number_mapping:
        all.append(row + [item_number_mapping[item_no]])
        seen.add(item_no)
# after this for loop, `all` is identical to the list comp version
unmatched_items = [item_number_mapping[item_num] for item_num in
                   set(item_number_mapping.keys()) - seen]

这是一个旁白,但不要将嵌套的
用于
表达式。您可以使用逗号链接它们,例如,
将open('file1.txt')作为file1,将open('file2.txt')作为file2,…
谢谢您提供的数据。你能给我们看看你实际得到的产量吗?我认为预期的输出很清楚,sitemap_bp.csv的更新片段不够,目前正在使用
\d{4、}\d{4、}\d{4、}\d{4、}\d{4、}\d{4、}\d{4、}\d{4、}\d{4、}\d{4、}\d{4、}\d{4、}\d{4、}\d{4、}\d{4、}\d{4、{4、}\d}来捕获新的reg4类型。只要做
r“\d{4,}(?:\d{4,})*”
对我有用!TY,在'all=[]中我需要的一件事是没有相应匹配的URL(来自sitemap),最好的方法是什么?我需要遍历站点地图吗?@EliquidVape您是指
sitemap\u bp.csv
中的所有URL还是
mobilesitemap browse.csv
?站点地图\u bp.csv中的所有URL