简体   繁体   English

我如何使用python在csv中搜索一行并将其放入另一个文件中:

[英]how do i search for a row in a csv in python and putting it in a different file using this code:

import re
import csv

with open('a.csv','r') as f:
    r = csv.reader(f)
    dict2 = {row[0]: row[1:] for row in r}

with open('b.csv', 'r') as f:
    r = csv.reader(f)
    dict1 = {row[0]: row[1:] for row in r}

fine = {k: (dict1[k], dict2[k]) for k in set(dict1).intersection(dict2)}

with open('c.csv', 'w') as f:
    w = csv.writer(f)
    for value in fine.items():
        w.writerow(value)

please help, I cant find how to input it into another csv, I am doing this for school so please can you help me solve the way to actually change it so that is searches for a row that includes certain words in the first two csv s 请帮忙,我找不到如何将其输入到另一个csv中,我正在学校上学,所以请您能帮我解决实际更改它的方法,以便在前两个csv中搜索包含某些单词的行

One of those? 其中之一?

for value in fine.items()::
    unpacked_value = [elem for lst in value for elem in lst]
    w.writerow(unpacked_value)
for value in fine:
    for row in fine[value]:
        w.writerow((value, row))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何获取python从CSV文件中删除搜索的行 - How do i get python to remove the row i search for from csv file 如何在 csv 文件中搜索特定字段然后在 Python 中打印整行 - How do I search for a specific field in a csv file then print the entire row in Python 如何循环遍历 CSV 文件中的一行 Python? - How do I loop through one row of a CSV file in Python? 使用python,你如何选择一个csv文件的随机行? - Using python, how do you select a random row of a csv file? 如何使用Python在CSV文件中跳过多行 - How do I skip more than 1 row in a CSV file using Python 我有一个csv文件,我想将csv文件的每一行提取到不同的csv文件中。 我怎样才能做到这一点? - I have a csv file and i want to extract each row of csv file into different csv file . how can i do that? 如何从python中的不同输入打印到CSV文件 - how do i print to a a CSV file from different inputs in python 您如何在python中搜索CSV文件? - How do you search a CSV file in python? 如何循环并将我的代码附加到 CSV 文件? - Python - How do I loop and append my code to a CSV file? - Python 我如何使用 jupyter 笔记本的 python 代码从网站下载 csv 文件 - how do i download the csv file from a website using python code for my jupyter notebook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM