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 :’)
JavaScript
x
28
28
1
DSNY SR# is: 2018EC0000000
2
PROGRAM: sampleNYC
3
CONTACT-ROLE: property manager
4
CONTACT-FIRST-NAME: Peter
5
CONTACT-LAST-NAME: Parker
6
CONTACT-EMAIL: peteyparks@yahoo.com
7
CONTACT-PHONE: (555) 518-2578
8
CONTACT-PHONE-TYPE: Mobile
9
SITE-ADDRESS-1: 0000 00 STREET BROOKLYN 11219
10
SITE-BORO: 3-BROOKLYN
11
SITE-TYPE: rental
12
SITE-UNITS: 16
13
MGMT-COMPANY-NAME: Parker Realty Corp
14
MGMT-COMPANY-CONTACT-FIRST-NAME: Peter
15
MGMT-COMPANY-CONTACT-LAST-NAME: Parker
16
MGMT-COMPANY-CONTACT-TITLE: Manager
17
MGMT-COMPANY-ADDRESS-1: 0000 23rd Street
18
MGMT-COMPANY-City: Brooklyn
19
MGMT-COMPANY-State: NY
20
MGMT-COMPANY-ZIP: 11219
21
MGMT-COMPANY-CONTACT-EMAIL: peteyparks@yahoo.com
22
MGMT-COMPANY-CONTACT-PHONE: (555) 518-2578
23
MGMT-COMPANY-CONTACT-PHONE-TYPE: Mobile
24
MGMT-COMPANY-CONTACT-NOTIFICATION: YES
25
REG-SOURCE: Word of Mouth
26
REG-SOURCE-DETAIL:
27
TERMS-CONDITIONS: Yes
28
Advertisement
Answer
You can use read_table
:
Read general delimited file into DataFrame.
For example,
JavaScript
1
9
1
import pandas as pd
2
3
df = pd.read_table(
4
'/path/to/file.txt',
5
sep=':',
6
header=None,
7
names=['col1','col2']
8
)
9