[IBM]Python for Data Science, AI & Development - Working with different file formats

2021. 5. 11. 19:18Data science/Python

반응형

csv, xml, json, xlsx 

 

Pandas Library: able to easily read the different files, using Dataframe

 

1. csv file : using pandas (import pandas)

df.columns =['Name'..] 

 

2. JSON Files : using json (import json) 

import json 
with open('filessample.json', 'r') as openile:
	json_object = json.load(openfile)
print(json_object)

3. XML file (import pandas and xml.etree.ElementTree (to parse the XML file) 

import pandas as pd 
import zml.etree.ElementTree as etree
tree = etree.parse("fileExample.xml")
root = tree.getroot()
columns - ["Name", "Phone Number", "Birthday"]
df=pd.DataFram(columns = columns)
for node in root:
	name = node.find("name").text
    phonenumber=node.find("phonenumber").text
    birthday=node.find("birthday").text
    
  df = df.append(pd.Series([name, phonenumber, birthday], index= columns)
  ...., ignore_index=True)
  
  #document to collect the necessary data and append the data to a data frame
  

 

반응형