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 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 循环后,它会尝试再次迭代 sitemap 但不能,因为文件已经耗尽。最小的改变是:

        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_bp.csv 文件,而不是像这样只迭代一次我的代码。

编辑评论中的每个问题

如果您需要获取 sitemap_bp.csv 中与 mobilesitemap-browse.csv 不对应的网址列表,那么您可能会得到最好的服务通过为您看到的所有项目创建一个集合,然后使用集合操作来获取未见过的项目。这需要一些修改,但是...

# 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]

关于python - 如果行包含字符串追加行,则为 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28999222/

相关文章:

python - 向 GEKKO 添加输入

python - 没有步进函数的python中的ECDF?

php - 将 csv 导入 mySQL 数据库时出现西里尔字母编码问题

python - 将具有不同大小的单个数组的二维 numpy 字符串数组保存到 csv 文件

python - 如果 csv 存储为变量,如何使用 pandas read_csv() 方法?

python - 在 Python 中将时间戳转换为 rfc 3339

python - 如何在具有匹配字符串的嵌套列表中查找索引的最小值和最大值?

java - Python到java图像处理翻译

mysql - secure-file-priv 空集

python - 高效地从巨大的 CSV 文件中读取数据