首页 > 解决方案 > 根据没有熊猫的现有列在 csv 中创建列

问题描述

我在 CSV Image Link中有以下结构

在此处输入图像描述

要存储在 CSV 中的值:

hashes = [('layer_1', 'start_1','40' ,'34'), ('layer_2', 'start_2','12','45')]

我想要的是

我需要根据 id 列将哈希值写入 csv

**id**
Street_1
S1L4
S1L3
S1L2
S1L1
Street_2
S2L3
S2L2
Street_3
S3L3
S3L2
Street_4
S4L3
S4L2
Street_5
S5L3
S5L4
S5L2

条件:

我试过的

import csv
hashes = [('layer_1', 'start_1','40' ,'34'), ('layer_2', 'start_2','12','45')]
in_file = open(r'C:\Users\user\Desktop\python\input.csv','r')
reader = csv.reader(in_file)
out_file = open(r'C:\Users\user\Desktop\python\output.csv', 'w')
writer = csv.writer(out_file)
for row in reader:
    
    temp = p[0]
    temp2 = p[1]
    temp3 = p[2]
    temp4= row[14]
    print(temp4)
    if temp4[-1] == int(temp[-1]) and temp4[-3] == int(temp2[-1]):
        writer.writerow(temp3)
   
    
in_file.close()    
out_file.close()

我期待以下输出

id           percentage     volume
Street_1        
S1L4        
S1L3        
S1L2        
S1L1              40         34
Street_2        
S2L3        
S2L2              12         45
Street_3        
S3L3        
S3L2        
Street_4        
S4L3        
S4L2        
Street_5        
S5L3        
S5L4        
S5L2    

标签: pythonpython-3.xcsv

解决方案


为了实现这一点,我编写了一个小函数,从哈希中提取 id 以创建一个字典,以便查找以匹配输入文件。如果输入文件中的 id 匹配,它会将来自该哈希的数据添加到行中,并将其写出。

更新 为了符合 OP 关于id字段前行的更新问题,进行了两项更改:

  1. in_id = row[ 0 ]--> in_id = row[ -1 ]使用最后一列作为id.
  2. out_row = [ in_id ]-->out_row = row保留行数据,并将哈希数据附加到它。通过此更新,out_row可以删除变量并可以直接在 上进行操作row,但是我在此示例中保留了它以与以前的版本保持一致。
import re
import csv

def hash_id( h ):
    """
    :params h: Hash to parse.
    :returns: Id of the hash. 
    """
    layer_pattern = 'layer_(\d+)'
    start_pattern = 'start_(\d+)'
    
    layer_match = re.match( layer_pattern, h[ 0 ] )
    start_match = re.match( start_pattern, h[ 1 ] )
    
    layer = layer_match.group( 1 )
    start = start_match.group( 1 )
    
    idh = f'S{ start }L{ layer }'
    return idh

#--- main script ---

# create id-hash dictionary for look ups
id_hashes = { hash_id( h ): h for h in hashes }

with open( 'input.csv', newline = '' ) as infile:
    reader = csv.reader( infile )
    
    with open( 'output.csv', 'w', newline = '' ) as outfile:
        writer = csv.writer( outfile )
        
        for row in reader:
            in_id = row[ -1 ] # look up id from last column of the input file
            
            out_row = row
            if in_id in id_hashes:
                # input id found in hashes, add data
                matched_hash = id_hashes[ in_id ]
                out_row += matched_hash[ 2: ]
            
            # write output row
            writer.writerow( out_row )

推荐阅读