python - 根据没有熊猫的现有列在 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
条件:
如果 id 列包含 Street_1,Street_2 等等...我需要将其保留为空白
在 id S1L1 中表示 Start_1 和 Layer_1。如果在下面的列表中找到这些值
('layer_1', 'start_1','40' ,'34') 然后我需要写入对应 S1L1 行的值 40 和 34。
我试过的
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
解决方案
为了实现这一点,我编写了一个小函数,从哈希中提取 id 以创建一个字典,以便查找以匹配输入文件。如果输入文件中的 id 匹配,它会将来自该哈希的数据添加到行中,并将其写出。
更新
为了符合 OP 关于id
字段前行的更新问题,进行了两项更改:
in_id = row[ 0 ]
-->in_id = row[ -1 ]
使用最后一列作为id
.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 )
推荐阅读
- testng - 当使用 @test 注释运行我的代码时,我的代码返回以下错误消息
- php - 意外的循环结果
- asp.net-core - 找不到类型或命名空间 SigninManager<> 身份 3.1.1 Mediatr 8.0.0
- google-maps - 如何在仅包含地标文件夹的 KML/KMZ 文件中使用 NetworkLink refreshMode?
- python - 我无法在 anaconda python3.6 和 spyder 上成功运行 tensorflow-gpu
- ruby-on-rails - 如何在 PostgreSQL + Rails 上获得不同两列的计算值?
- reactjs - 如何根据父组件的更改重置组件的状态?
- asp.net - 使用 AngularJS 将数据从视图传递到 net-mvc 控制器
- javascript - Angular:ngOnInit 不加载我的函数?
- networking - 物理通信的通用“管道”概念的名称?