0

I have a CSV file format: id ,name..., one of his rows contain an XML file like this

<?xml version=""1.0"" encoding=""UTF-8""?><session><appUsage packageName=""com.facebook.katana"" name=""Facebook"" startTime=""1603137470472""><state name=""[]"" className=""android.widget.FrameLayout"" duration=""1639"" interactionCount=""0"" orientation=""1""/><state name=""[Facebook]"" 

I am trying to extract the packageName and statTime.Howa can i do ? Please help

Equinox
  • 6,483
  • 3
  • 23
  • 32
  • 1
    try using,[`xmltodict`](https://stackoverflow.com/a/40157811/4985099) to convert xml to dict then use [`csv`](https://stackoverflow.com/a/10373268/4985099) module to dump to file. – sushanth Nov 03 '20 at 12:04

1 Answers1

0

Use XML parser and extract the data you are looking for.

import xml.etree.ElementTree as ET

xml = '''<?xml version="1.0" encoding="UTF-8"?>
               <session>
                   <appUsage packageName="com.facebook.katana" name="Facebook" startTime="1603137470472">
                        <state name="[]" className="android.widget.FrameLayout" duration="1639" interactionCount="0" orientation="1"/>
                        <state name="[Facebook]"/>
                    </appUsage>
               </session>
'''
root = ET.fromstring(xml)
package_name = root.find('.//appUsage').attrib['packageName']
print(package_name)

output

com.facebook.katana
balderman
  • 22,927
  • 7
  • 34
  • 52
  • the above code will help if you have separate string. but are you able to read csv that contains xml string and able to create proper data frame – Nagendra Nov 16 '22 at 16:11