简体   繁体   English

Python 程序:循环输入 csv 文件中的字符串; 在另一个 csv 文件中搜索匹配的字符串; 将匹配的行内容返回给另一个 csv

[英]Python program: loop around strings from input csv file; search the matching string in another csv file; return matching row content to another csv

I have the first excel file user_details.csv我有第一个 excel 文件user_details.csv

在此处输入图像描述

I have another excel file user_mangers.csv我还有另一个 excel 文件user_mangers.csv

在此处输入图像描述

I need to loop and select each of the users from user_details.csv one by one, find the corresponding manager from user_managers.csv and append the details into a new csv file, say user_details_managers.csv like below.我需要循环和 select 每个用户从user_details.csv一个一个地找到相应的经理从user_managers.csv和 append 的细节到一个新的 csv 文件,说user_details_managers.8814148986

在此处输入图像描述

Can be easily done with pandas :可以使用pandas轻松完成:

import pandas as pd

user_details = pd.read_csv('user_details.csv')
user_managers = pd.read_csv('user_mangers.csv')
merged_df = user_details.merge(user_managers, how='left', left_on='user', right_on='username')
merged_df.to_csv('user_details_managers.csv')

So what happens here?那么这里发生了什么? I used pandas to read both csv files into dataframes.我使用 pandas 将两个 csv 文件读入数据帧。 Then we merged them into a single dataframe: on the "left" dataframe (here is user_details) we merged on "user" column, and on the "right" dataframe (user_mangers) it took the "username" column.然后我们将它们合并为一个 dataframe:在“左侧”dataframe(这里是 user_details)我们合并了“user”列,在“右侧”dataframe(user_mangers)合并了“username”列。 It mean it will combine each "user" from the left with "username" from the right.这意味着它将把左边的每个“用户”和右边的“用户名”组合起来。

The result is a single dataframe, and I use .to_csv() to save it back as csv.结果是一个dataframe,我用.to_csv()把它存回csv。

PS - Please review how to ask guide, as your question is missing what you tried - StackOverflow community is not here to write code for you but to help you. PS - 请查看如何询问指南,因为您的问题缺少您尝试过的内容 - StackOverflow 社区不是在这里为您编写代码而是为您提供帮助。 Although I answered your question (cause it's quick), usually the community will not help you if your question doesn't meet the minimum standard.虽然我回答了你的问题(因为它很快),但如果你的问题不符合最低标准,社区通常不会帮助你。

In case you wanted to have less dependencies, this is how you do it with the standard csv library:如果您想减少依赖性,这就是使用标准csv库的方法:

import csv

# collect the corresponding items by username
user_details_managers = {}

with open("./user_details.csv") as details_fp:
    reader = csv.reader(details_fp)
    # read first row to skip headers
    next(reader)
    for row in reader:
        user, details = row
        user_details_managers[user] = [user, details]

with open("./user_managers.csv") as managers_fp:
    reader = csv.reader(managers_fp)
    # read first row to skip headers
    next(reader)
    for row in reader:
        username, firstname, lastname = row
        if username in user_details_managers:
            user_details_managers[username].extend([firstname, lastname])


with open("./user_details_managers.csv", "w") as details_managers_fp:
    writer = csv.writer(details_managers_fp)

    # write headers
    writer.writerow(["user", "details", "manager firstname", "manager lastname"])

    # write combined rows
    writer.writerows(user_details_managers.values())

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

相关问题 Python 在 csv 列中搜索字符串并在另一个 csv 文件中写入行 - Python search for string in csv columns and write row in another csv file Python中的正则表达式,用于匹配CSV文件中的字符串 - Regular expression in Python for matching strings in a CSV file 如何在 Python 的 csv 文件中查找匹配字符串? - How to find matching string in csv file in Python? python将变量中的CSV值与CSV文件匹配 - python matching CSV values from a variable against a CSV file issue 从匹配第一个元素的两个不同CSV文件中复制行,然后在Python中一个接一个地粘贴到第三个CSV文件中 - Copy lines from two different CSV files matching first element and paste it in 3rd CSV file one after another in Python 将值从一个csv文件匹配到另一个,并使用pandas / python替换整个列 - Matching values from one csv file to another and replace entire column using pandas/python 如何根据 Python 中的匹配值将信息从一个 CSV 文件添加到另一个文件? - How do I add information from one CSV file to another based off of matching values in Python? Python3:在csv文件中搜索字符串并返回行和列 - Python3: search csv file for string and return row and column 编辑csv文件Python 3 - 将文件内容与打印输出匹配 - Editing csv file Python 3 - matching file content to the print output CSV文件上的模式匹配 - pattern matching on CSV file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM