Skip to content
Advertisement

I’m trying to create a table from text

I want to create a table with two columns separated by “:”. So the capitalized words as the first column and everything after the “:” as the second column. I was originally tried to do this from a PDF but that wasn’t working so I copied it to a text file thinking it might be easier that way. I’m very new to Python but this is something I’ll need to know to make transferring data to a spreadsheet easier. How would I do that?? I’m pretty lost :’)

DSNY SR# is: 2018EC0000000
PROGRAM: sampleNYC
CONTACT-ROLE: property manager
CONTACT-FIRST-NAME: Peter
CONTACT-LAST-NAME: Parker
CONTACT-EMAIL: peteyparks@yahoo.com
CONTACT-PHONE: (555) 518-2578
CONTACT-PHONE-TYPE: Mobile
SITE-ADDRESS-1: 0000 00 STREET BROOKLYN 11219
SITE-BORO: 3-BROOKLYN
SITE-TYPE: rental
SITE-UNITS: 16
MGMT-COMPANY-NAME: Parker Realty Corp
MGMT-COMPANY-CONTACT-FIRST-NAME: Peter
MGMT-COMPANY-CONTACT-LAST-NAME: Parker
MGMT-COMPANY-CONTACT-TITLE: Manager
MGMT-COMPANY-ADDRESS-1: 0000 23rd Street
MGMT-COMPANY-City: Brooklyn
MGMT-COMPANY-State: NY
MGMT-COMPANY-ZIP: 11219
MGMT-COMPANY-CONTACT-EMAIL: peteyparks@yahoo.com
MGMT-COMPANY-CONTACT-PHONE: (555) 518-2578
MGMT-COMPANY-CONTACT-PHONE-TYPE: Mobile
MGMT-COMPANY-CONTACT-NOTIFICATION: YES
REG-SOURCE: Word of Mouth
REG-SOURCE-DETAIL:
TERMS-CONDITIONS: Yes

Advertisement

Answer

You can use read_table:

Read general delimited file into DataFrame.


For example,

import pandas as pd

df = pd.read_table(
    '/path/to/file.txt', 
    sep=':', 
    header=None, 
    names=['col1','col2']
)
Advertisement