根据没有熊猫的现有列在 csv 中创建列

时间:2021-02-07 17:12:32

标签: python python-3.x csv

我在 CSV 中有以下结构 Image Link

enter image description here

要存储在 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

条件:

  • 如果 id 列包含 Street_1、Street_2 等等......我需要将其留空

  • In id S1L1 代表 Start_1 和 Layer_1。如果在下面的列表中找到这些值

    ('layer_1', 'start_1','40','34') 然后我需要写值 40 和 34 对应的 S1L1 行。

我尝试了什么

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    

1 个答案:

答案 0 :(得分:0)

为了实现这一点,我编写了一个小函数,从散列中提取 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 )