Чтение текстового файла в Anaconda? - PullRequest
0 голосов
/ 31 октября 2019

Попытка прочитать файл .txt в моей записной книжке Jupyter.

Это мой код:

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

Вот пример моего текстового файла:

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

Не уверен, как настроить это так, чтобы данные читались правильно. В идеале, я хотел бы, чтобы фрейм данных, где каждая категория представляет собой отдельный столбец, а затем все записи в документе являются строками. Спасибо!

1 Ответ

0 голосов
/ 31 октября 2019

Мое регулярное выражение не так быстро, как должно быть, но приведенное ниже может работать, пока данные остаются в той же форме, а имена столбцов не дублируются в других строках:

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})
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...