【问题标题】:adding column 2 from a group of text files to 1 text file将一组文本文件中的第 2 列添加到 1 个文本文件
【发布时间】:2013-12-28 12:26:19
【问题描述】:

我有一组文本文件,我希望将每个文本文件的第二列按顺序添加到一个新的文本文件中。这些文件是制表符分隔的,格式如下:

name dave
age 35
job teacher
income 30000

我已经生成了一个文件,其中一个文件的第一列代替了第二列,希望能简化问题:

0 name
0 age 
0 job
0 income 

我有大量这些文件,并希望将它们全部放在制表符分隔的文本文件中,例如:

name dave mike sue
age 35 28 40
job teacher postman solicitor
income 30000 20000 40000

我有一个文本文件,其中只包含名为 all_libs.txt 的所有文件的名称

到目前为止,我已经写了:

#make a sorted list of the file names
with open('all_libs.txt', 'r') as lib:
     people = list([line.rstrip() for line in lib])
     people_s = sorted(people)

i=0

while i< len(people_s):
    with open(people_s[i]) as inf:
        for line in inf:                
            parts = line.split() #split line into parts
            if len(parts) > 1:    #if more than 1 discrete unit in parts
                with open("all_data.txt", 'a') as out_file: #append column2 to all_data
                    out_file.write((parts[1])+"\n")

i=i+1 #go to the next file in the list

在打开每个新文件时,我想将其添加为新列,而不是仅作为新行追加。真的很感激任何帮助吗?我意识到像 SQL 这样的东西可能会让这变得简单,但我从未使用过它,也没有时间致力于 SQL 的学习曲线。非常感谢。

【问题讨论】:

    标签: python text multiple-columns


    【解决方案1】:

    这是一种非常不切实际的数据存储方式——每条记录都分布在所有行中,因此在读取文件和(如您所见)添加记录时很难重建记录。

    您应该使用标准格式,例如 csv 或(在这种情况下更好)json

    例如,您可以像这样将它们保存为 CSV:

    name,age,job,income
    dave,35,teacher,30000
    mike,28,postman,20000
    sue,40,solicitor,40000
    

    阅读此文件:

    >>> import csv
    >>> with open("C:/Users/Tim/Desktop/people.csv", newline="") as infile:
    ...     reader = csv.DictReader(infile)
    ...     people = list(reader)
    

    现在你有一个人员列表:

    >>> people
    [{'income': '30000', 'age': '35', 'name': 'dave', 'job': 'teacher'}, 
     {'income': '20000', 'age': '28', 'name': 'mike', 'job': 'postman'}, 
     {'income': '40000', 'age': '40', 'name': 'sue', 'job': 'solicitor'}]
    

    您可以轻松访问:

    >>> for item in people:
    ...     print("{0[name]} is a {0[job]}, earning {0[income]} per year".format(item))
    ...
    dave is a teacher, earning 30000 per year
    mike is a postman, earning 20000 per year
    sue is a solicitor, earning 40000 per year
    

    现在添加新记录只需将它们添加到文件末尾即可:

    >>> with open("C:/Users/Tim/Desktop/people.csv", "a", newline="") as outfile:
    ...    writer = csv.DictWriter(outfile,
    ...                            fieldnames=["name","age","job","income"])
    ...    writer.writerow({"name": "paul", "job": "musician", "income": 123456,
    ...                     "age": 70})
    

    结果:

    name,age,job,income
    dave,35,teacher,30000
    mike,28,postman,20000
    sue,40,solicitor,40000
    paul,70,musician,123456
    

    或者您可以将其保存为 JSON:

    >>> import json
    >>> with open("C:/Users/Tim/Desktop/people.json", "w") as outfile:
    ...     json.dump(people, outfile, indent=1)
    

    结果:

    [
     {
      "income": "30000", 
      "age": "35", 
      "name": "dave", 
      "job": "teacher"
     }, 
     {
      "income": "20000", 
      "age": "28", 
      "name": "mike", 
      "job": "postman"
     }, 
     {
      "income": "40000", 
      "age": "40", 
      "name": "sue", 
      "job": "solicitor"
     }
    ]
    

    【讨论】:

    • 感谢您的帖子。不幸的是,它不是我的数据,它来自其他人的管道,这是格式,所以我需要解决它。
    • @user3062260:你写了“我有这些文件......” - 好吧,它们的格式不太理想,但很容易阅读。然后你写了“我想把它们放在这样的文件中......” - 为什么?我的解决方案涉及第二部分,如何以有用的方式聚合数据,看来你是可以控制这部分的人。
    【解决方案2】:
    file_1 = """
    name dave1
    age 351
    job teacher1
    income 300001"""
    
    file_2 = """
    name dave2
    age 352
    job teacher2
    income 300002"""
    
    file_3 = """
    name dave3
    age 353
    job teacher3
    income 300003"""
    
    template = """
    0 name
    0 age
    0 job
    0 income"""
    

    假设上面是从文件中读取的

    _dict = {}
    
    
    def concat():
        for cols in template.splitlines():
            if cols:
                _, col_name = cols.split()
                _dict[col_name] = []
    
        for each_file in [file_1, file_2, file_3]:
            data = each_file.splitlines()
            for line in data:
                if line:
                    words = line.split()
                    _dict[words[0]].append(words[1])
    
        _text = ""
    
        for key in _dict:
            _text += '\t'.join([key, '\t'.join(_dict[key]), '\n'])
    
        return _text
    
    print concat()
    

    输出

    job teacher1    teacher2    teacher3    
    age 351 352 353 
    name    dave1   dave2   dave3   
    income  300001  300002  300003  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-08
      • 1970-01-01
      • 1970-01-01
      • 2017-03-28
      • 1970-01-01
      • 2011-07-24
      • 2016-10-20
      • 1970-01-01
      相关资源
      最近更新 更多