Reputation: 303
I am attempting to read in CSV data and map it to the following XML structure. In my first attempt, I have used % operator for string formatting, which works.
import pandas as pd
import uuid
df = pd.read_csv('media.csv', sep=',')
def convert_row(row):
return """<video>
<videoType fileUID="%s" name="">
<locationType></locationType>
<type>%s</type>
<format>%s</format>
<year>%s</year>
</videoType>
</video>""" % (row[0], row[1], row[2], row[3])
print '\n'.join(df.apply(convert_row, axis=1))
However, I am hoping to populate the fileUID="%s"
with a generated uuid, that I can then reference elsewhere. I'm not able to get this to work.
I tried adding u = str(uuid.uuid4())
just before the return statement and update % (u, row[0], row[1], row[2], row[3])
I get a 'not all arguments converted during string formatting' error
So I attempted to use f-strings formatting
import pandas as pd
import uuid
df = pd.read_csv('media.csv', sep=',')
def convert_row(row):
return f"""<video>
<videoType fileUID="{u}" name="">
<locationType></locationType>
<type>{row[0]}</type>
<format>{row[1]}</format>
<year>{row[2]}</year>
</videoType>
</video>"""
print '\n'.join(df.apply(convert_row, axis=1))
and get another error stating invalid syntax in regards to the closing """
I guess my questions are which string formatting style is the best option when dealing with UUID and what is wrong with my code? Plus, if I wish to reference the generated uuid in other generated xml structures - I will ultimately create an xml file with the multiple generated xml structure content.
Thank you for any assistance
Upvotes: 1
Views: 1381
Reputation: 3616
To @deceze's point you were missing some formatting in your code shown.
I believe what your looking for is something like this?
"""UUID Example."""
import pandas as pd
import uuid
df = pd.read_csv('media.csv', sep=',')
def convert_row(row):
"""Convert Row Example."""
u = str(uuid.uuid4())
return """<video>
<videoType fileUID={} name=\"\">
<locationType></locationType>
<type>{}</type>
<format>{}</format>
<year>{}</year>
</videoType>
</video>""".format(u, row[0], row[1], row[2], row[3])
print("\n".join(df.apply(convert_row, axis=1)))
$ python3 uuid_testing.py
<video>
<videoType fileUID=d07ea048-a08f-444c-9182-7fff3a825dcc name="">
<locationType></locationType>
<type>2</type>
<format>b</format>
<year>f</year>
</videoType>
</video>
<video>
<videoType fileUID=4b058e99-457e-4b26-ac03-593c51e6cb1e name="">
<locationType></locationType>
<type>3</type>
<format>c</format>
<year>g</year>
</videoType>
</video>
<video>
<videoType fileUID=8d04f6e6-1b5b-4103-a42a-aaa9c45d7bc5 name="">
<locationType></locationType>
<type>4</type>
<format>d</format>
<year>h</year>
</videoType>
</video>
Github: Code was added to my Repo.
Upvotes: 1