【问题标题】:Iterate through two files to create a new file that has fields from second file appended to fields of first file遍历两个文件以创建一个新文件,该文件将第二个文件的字段附加到第一个文件的字段
【发布时间】:2019-10-02 20:00:29
【问题描述】:

我是 Python 新手。我尝试使用@mgilson@endolith@zackbloom zack's example 中的答案中的逻辑

  1. 我在主记录的第一个字段前面放置了一堆空白列。
  2. 我的 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


【解决方案1】:

extend 属性不适用于 build_set 创建的元组。元组是不可变的,但可以使用普通的 Python 字符串函数连接或切片。

例如:

with open('C:\\temp\\oz\\loads_pudo_out.csv', 'w') as out_file:
    for pri in set_primary_records :
        for sub in set_sub_records :
            if sub[1] == pri[0] :
                record = pri + sub
                out_file.write(str(record)[1:-1] + '\n')

这与上面的代码相同,只是修改为允许元组连接。在写入行中,我们将记录转换为字符串,并在附加“\n”之前去掉开始和结束括号。也许有更好/更漂亮的方法可以做到这一点,但我也是 Python 新手。

编辑: 要获得您期望的输出,需要进行一些更改:

# On this line, remove the sort() as we do not wish to change tuple item order..
found.add(tuple(line.split(';')))

...

with open('C:\\temp\\loads_out.csv', 'w') as out_file:
    for pri in set_primary_records:
        record = pri                        # record tuple is set in main loop
        for sub in set_sub_records:
            if sub[1] == pri[0]:
                record += sub               # for each match, sub appended to record
        out_file.write(str(record) + '\n')  # removed stripping of brackets

【讨论】:

  • 你的更新让我更近了一步。但是,我似乎正在输出一个 cartisan 产品(每个内部记录都连接到每个外部记录)。
  • @Dr.EMG 更新以反映您期望的输出(我希望)。删除了 build_set 中元组的排序。 “记录”现在是通过附加子匹配来构建的。
猜你喜欢
  • 2015-03-07
  • 1970-01-01
  • 2014-05-01
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 1970-01-01
  • 2021-01-08
  • 1970-01-01
相关资源
最近更新 更多