【问题标题】:Reading txt file into Anaconda?将txt文件读入Anaconda?
【发布时间】:2019-10-31 20:26:22
【问题描述】:

尝试将.txt 文件读入我的 Jupyter 笔记本。

这是我的代码:

acm = pd.read_csv('outputacm.txt', header=None, error_bad_lines=False)
print(acm)

这是我的 txt 文件的示例:

2244018
#*OQL[C++]: Extending C++ with an Object Query Capability.
#@José A. Blakeley
#year1995
#confModern Database Systems
#citation14
#index0
#arnetid2

#*Transaction Management in Multidatabase Systems.
#@Yuri Breitbart,Hector Garcia-Molina,Abraham Silberschatz
#year1995
#confModern Database Systems
#citation22
#index1
#arnetid3

#*Overview of the ADDS System.
#@Yuri Breitbart,Tom C. Reyes
#year1995
#confModern Database Systems
#citation-1
#index2
#arnetid4

而不同的符号应该对应:

#* --- paperTitle
#@ --- Authors
#year ---- Year
#conf --- publication venue
#citation --- citation number (both -1 and 0 means none)
#index ---- index id of this paper
#arnetid ---- pid in arnetminer database
#% ---- the id of references of this paper (there are multiple lines, with each indicating a reference)
#! --- Abstract

不知道如何设置才能正确读取数据。理想情况下,我想要一个数据框,其中每个类别都是不同的列,然后文档中的所有条目都是行。谢谢!

【问题讨论】:

    标签: python pandas dataframe text jupyter


    【解决方案1】:

    我的正则表达式没有达到应有的速度,但只要数据保持相同的形式并且列名在其他行中没有重复,下面的方法可能会起作用:

    import re
    import pandas as pd
    
    path = r"filepath.txt"
    
    f = open(path, 'r')
    
    year = []
    confModern = []
    #continue for all columns
    
    for ele in f:
        if len(re.findall('year', ele)) > 0:
           year.append(ele[5:])
        if len(re.findall('confModern', ele)) > 0:
           year.append(ele[12:])
        # continue for all columns with the needed string
    
    df = pd.DataFrame(data={'year' : year ...#continue for each list})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-13
      • 2019-03-26
      • 2015-01-30
      • 1970-01-01
      • 2014-05-06
      • 2012-10-28
      相关资源
      最近更新 更多