【发布时间】:2019-10-02 20:00:29
【问题描述】:
我是 Python 新手。我尝试使用@mgilson、@endolith 和@zackbloom zack's example 中的答案中的逻辑
- 我在主记录的第一个字段前面放置了一堆空白列。
- 我的 out_file 为空(很可能是因为两个文件中的列不匹配。
我该如何解决这个问题? 最终结果应如下所示:
('PUDO_id','Load_id','carrier_id','PUDO_from_company','PUDOItem_id';'PUDO_id';'PUDOItem_make')
('1','1','14','FMH MATERIAL HANDLING SOLUTIONS','1','1','CROWN','TR3520 / TWR3520','TUGGERS')
('2','2','7','WIESE USA','2','2','CAT','NDC100','3','2','CAT','NDC100','4','2',' 2 BATTERIES')
注意:在第 3 行的输出中,它将子文件中的 3 行附加到数组中,而前 2 行仅附加了子文件中的 1 行。这是由 pri[0] 和 sub[1] 中的值比较 TRUE 确定的。
这是我基于@Zack Bloom的代码:
def build_set(filename):
# A set stores a collection of unique items. Both adding items and searching for them
# are quick, so it's perfect for this application.
found = set()
with open(filename) as f:
for line in f:
# Tuples, unlike lists, cannot be changed, which is a requirement for anything
# being stored in a set.
line = line.replace('"','')
line = line.replace("'","")
line = line.replace('\n','')
found.add(tuple(sorted(line.split(';'))))
return found
set_primary_records = build_set('C:\\temp\\oz\\loads_pudo.csv')
set_sub_records = build_set('C:\\temp\\oz\\pudo_items.csv')
record = []
with open('C:\\temp\\oz\\loads_pudo_out.csv', 'w') as out_file:
# Using with to open files ensures that they are properly closed, even if the code
# raises an exception.
for pri in set_primary_records :
for sub in set_sub_records :
#out_file.write(" ".join(res) + "\n")
if sub[1] == pri [0] :
record = pri.extend(sub)
out_file.write(record + '\n')
示例源数据(主要记录):
PUDO_id;"Load_id";"carrier_id";"PUDO_from_company"
1;"1";"14";"FMH MATERIAL HANDLING SOLUTIONS"
2;"2";"7";"WIESE USA"
示例源数据(子记录):
PUDOItem_id;"PUDO_id";"PUDOItem_make"
1;"1";"CROWN";"TR3520 / TWR3520";"TUGGERS"
2;"2";" CAT";"NDC100"
3;"2";"CAT";"NDC100"
4;"2";" 2 BATTERIES"
5;"11";"MIDLAND"
【问题讨论】:
-
为什么 PHP 在标题中,并且链接到问题/答案比标记人们的屏幕名称要好得多,我,一方面,我不会查看他们的答案历史并试图找出哪个您所指的帖子。
-
如何引用特定帖子?
-
我添加了一个指向上述示例的链接。 (见Zack's Example)。
标签: python set iteration text-files