当前位置: 首页 > 知识库问答 >
问题:

Python csv:按分隔符将列拆分为列,然后再拆分为行

茹元魁
2023-03-14

我在csv文件中有一个列,其中包含此格式的人员详细信息:

+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|  Team  |                                                                                                Members                                                                                                 |
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Team 1 | OK-10:Jason:Jones:ID No:00000000:male:my notes                                                                                                                                                         |
| Team 2 | OK-10:Mike:James:ID No:00000001:male:my notes OZ-09:John:Rick:ID No:00000002:male:my notes                                                                                                             |
| Team 3 | OK-08:Michael:Knight:ID No:00000004:male:my notes2 OK-09:Helen:Rick:ID No:00000005:female:my notes3 OZ-10:Jane:James:ID No:00000034:female:my notes23 OK-09:Mary:Jane:ID No:00000023:female:my notes46 |
+--------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

实际csv格式:

"Team", "Members"                                                                                                 
 Team 1, OK-10:Jason:Jones:ID No:00000000:male:my notes                                                                                                                                                         
 Team 2, OK-10:Mike:James:ID No:00000001:male:my notes OZ-09:John:Rick:ID No:00000002:male:my notes                                                                                                             
 Team 3, OK-08:Michael:Knight:ID No:00000004:male:my notes2 OK-09:Helen:Rick:ID No:00000005:female:my notes3 OZ-10:Jane:James:ID No:00000034:female:my notes23 OK-09:Mary:Jane:ID No:00000023:female:my notes46

我想将它们拆分为一个新的csv文件,如下所示:

+-------+-------------+-------------+----------------+------------------+---------------+---------------+--------------+
| Team  | Member_Rank | Member_Name | Member_Surname | Member_ID_Method | Member_ID_Num | Member_Gender | Member_Notes |
+-------+-------------+-------------+----------------+------------------+---------------+---------------+--------------+
| Team1 | OK-10       | Jason       | Jones          | ID No            |      00000000 | male          | my notes     |
| Team2 | OK-10       | Mike        | James          | ID No            |      00000001 | male          | my notes     |
| Team2 | OZ-09       | John        | Rick           | ID No            |      00000002 | male          | my notes     |
+-------+-------------+-------------+----------------+------------------+---------------+---------------+--------------+

拆分详细信息:

拆分行分隔符:'O

拆分列分隔符:':',新csv文件中的列数是固定的

(一个团队可以包含多个成员,没有上限)

更新

通过使用@Adirio提供的此代码,我只从具有多个成员的字段中获取最后一个成员:

import csv
import re


members_split_regex = re.compile(r'(O[KZ]-\d+):([a-zA-Z0-9 ]+):([a-zA-Z0-9 ]+):([a-zA-Z0-9 ]+):([a-zA-Z0-9 ]+):([a-zA-Z0-9 ]+):([a-zA-Z0-9 ]+)(?= O[KZ]|$)')

with open('test.csv') as input_file, open('output_csv.csv', 'w', newline='') as output_file:
    csv_reader = csv.DictReader(input_file)
    fieldnames = csv_reader.fieldnames.copy()
    fieldnames.remove('Members')
    csv_writer = csv.DictWriter(output_file, extrasaction='ignore', fieldnames=fieldnames + ['Member_Rank', 'Member_Name', 'Member_Surname', 'Member_ID_Method', 'Member_ID_Num', 'Member_Gender', 'Member_Notes'])
    csv_writer.writeheader()
    for row in csv_reader:
        for member_tuple in members_split_regex.findall(row['Members']):
                member_dict = {}
                (
                    member_dict['Member_Rank'],
                    member_dict['Member_Name'],
                    member_dict['Member_Surname'],
                    member_dict['Member_ID_Method'],
                    member_dict['Member_ID_Num'],
                    member_dict['Member_Gender'],
                    member_dict['Member_Notes']
                ) = member_tuple
                print(row['Members'])
                print(member_tuple)
                member_dict.update(row)
                csv_writer.writerow(member_dict)

打印结果:

行['Members']-

OK-1:name1:sunrmae2:ID No:id1233123:男:Note 12 OK-10:name2:sunrame2:护照编号:asda3243242:女:Note 2 OZ-1:nma3:姓氏3:护照编号:asd213131:其他:注56

打印(member_tuple)-

(“OZ-1”、“nma3”、“surname3”、“护照号码”、“asd213131”、“其他”、“注56”)


共有2个答案

满勇军
2023-03-14

基于@DeepSpace答案,但添加了固定的正则表达式和新要求:

import csv
import re


members_split_regex = re.compile(r'(O[KZ]-\d+):([a-zA-Z0-9 ]+):([a-zA-Z0-9 ]+):([a-zA-Z0-9 ]+):([a-zA-Z0-9 ]+):([a-zA-Z0-9 ]+):([a-zA-Z0-9 ]+)(?= O[KZ]|$)')

with open('test.csv') as input_file, open('output_csv', 'w', newline='') as output_file:
    csv_reader = csv.DictReader(input_file)
    fieldnames = csv_reader.fieldnames.copy()
    fieldnames.remove('Members')
    csv_writer = csv.DictWriter(output_file, extrasaction='ignore', fieldnames=fieldnames + ['Member_Rank', 'Member_Name', 'Member_Surname', 'Member_ID_Method', 'Member_ID_Num', 'Member_Gender', 'Member_Notes'])
    csv_writer.writeheader()
    for row in csv_reader:
        for member_tuple in members_split_regex.findall(row['Members']):
            member_dict = {}
            (
                member_dict['Member_Rank'],
                member_dict['Member_Name'],
                member_dict['Member_Surname'],
                member_dict['Member_ID_Method'],
                member_dict['Member_ID_Num'],
                member_dict['Member_Gender'],
                member_dict['Member_Notes']
            ) = member_tuple
            member_dict.update(row)
            csv_writer.writerow(member_dict)

主要区别在于我正在从字典中删除该列,以便我们可以使用它来更新新字典。这样,我们不仅复制了“团队”列,还复制了其他非“成员”列。为此,还将复制读卡器的字段名,删除“Members”项,并将新的字段名添加到写卡器的字段名中。

使用的正则表达式不硬编码任何字段,允许姓名和姓氏中的空格、笔记中的大写O和不仅仅是8位数字的ID字段。

谭正谊
2023-03-14

假设这个输入CSV

Team,Members
Team 1,OK-10:Jason:Jones:ID No:00000000:male:my notes
Team 2,OK-10:Mike:James:ID No:00000001:male:my notes OZ-09:John:Rick:ID No:00000002:male:my notes
Team 3,OK-08:Michael:Knight:ID No:00000004:male:my notes2 OK-09:Helen:Rick:ID No:00000005:female:my notes3 OZ-10:Jane:James:ID No:00000034:female:my notes23 OK-09:Mary:Jane:ID No:00000023:female:my notes46

这可以通过regex,csv实现。DictReader和csv。DictWriter:

import csv
import re

output = []

members_split_regex = re.compile(r'(O[KZ]-\d+):([a-zA-Z0-9 ]+):([a-zA-Z0-9 ]+):([a-zA-Z0-9 ]+):([a-zA-Z0-9 ]+):([a-zA-Z0-9 ]+):([a-zA-Z0-9 ]+)(?= O[KZ]|$)')

with open('test.csv') as f:
    csv_reader = csv.DictReader(f)
    for row in csv_reader:
        team = row['Team']
        members = row['Members']
        split_members = members_split_regex.findall(members)
        for member in split_members:
                (member_rank, member_name, member_surname, member_id_method,
                 member_id_num, member_gender, member_notes) = member

                output.append({'Team': team, 'Member_Rank': member_rank, 'Member_Name': member_name,
                               'Member_Surname': member_surname, 'Member_ID_Method': member_id_method,
                               'Member_ID_Num': member_id_num, 'Member_Gender': member_gender,
                               'Member_Notes': member_notes})

with open('output_csv', 'w', newline='') as f:
    csv_writer = csv.DictWriter(f, fieldnames=['Team', 'Member_Rank', 'Member_Name', 'Member_Surname', 'Member_ID_Method', 'Member_ID_Num', 'Member_Gender', 'Member_Notes'])
    csv_writer.writeheader()
    csv_writer.writerows(output)

输出文件为

Team,Member_Rank,Member_Name,Member_Surname,Member_ID_Method,Member_ID_Num,Member_Gender,Member_Notes
Team 1,OK-10,Jason,Jones,ID No,00000000,male,my notes
Team 2,OK-10,Mike,James,ID No,00000001,male,my notes 
Team 2,OZ-09,John,Rick,ID No,00000002,male,my notes
Team 3,OK-08,Michael,Knight,ID No,00000004,male,my notes2 
Team 3,OK-09,Helen,Rick,ID No,00000005,female,my notes3 
Team 3,OZ-10,Jane,James,ID No,00000034,female,my notes23 
Team 3,OK-09,Mary,Jane,ID No,00000023,female,my notes46

 类似资料:
  • 问题内容: 我需要在SQL Server 2012中将一列中的字符串拆分为一个字符,并将每个字符串拆分成它自己的列。 例如:如果我有一个栏,我需要把它拆分成,,,,,与每个这些转化为自己列。 要拆分的列的长度可能会有所不同,因此我需要使其尽可能地动态。 问题答案: 您可以这样做: 输出: 这是动态版本:

  • 我的代码(用Java写的)运行一个外部工具并获得它的输出。该工具的输出如下所示: 我想把输出插入到一个结构中,这样就更容易使用了。我想了一个字符串矩阵。我知道如何通过空白空间将其拆分为数组: 但如何将每一行拆分为矩阵中的数组呢?

  • 问题内容: 我有一个小样本数据: 好像 我想用’-‘分隔符分隔列’V’并将其移至另一个名为’allele’的列 到目前为止,我尝试过的代码不完整,无法正常工作: 要么 问题答案: 与vectoried一起使用:

  • 问题内容: 一些外部数据供应商希望给我一个数据字段-管道分隔的字符串值,我觉得这很难处理。 没有应用程序编程语言的帮助,有没有办法将字符串值转换为行? 但是,存在一个困难,该字段具有未知数量的定界元素。 有问题的数据库引擎是MySQL。 例如: 问题答案: 它可能没有我最初想象的那么困难。 这是一种通用方法: 计算分隔符的出现次数 循环多次,每次获取一个新的定界值并将该值插入第二个表中。

  • 我有一个对象(Pos)与此模型的集合: 对象列表如下所示: 我想按beforeChangement或afterChangement==”字段拆分此对象列表要使用此格式(列表列表)

  • 问题内容: 我有一个具有这种结构的表。 我无法弄清楚我将使用哪种SQL查询来获得这样的结果集: 我正在尝试将三列分为三个单独的行。这可能吗? 问题答案: SELECT Y.UserID, Y.UserName, QuestionName = ‘AnswerToQuestion’ + X.Which, Response = CASE X.Which WHEN ‘1’ THEN AnswerToQue