Simple CSV to XML Conversion - Python

前端 未结 1 1009
温柔的废话
温柔的废话 2020-12-08 06:28

I am looking for a way to automate the conversion of CSV to XML.

Here is an example of a CSV file, containing a list of movies:

Here is the file in

1条回答
  •  悲哀的现实
    2020-12-08 06:42

    A possible solution is to first load the csv into Pandas and then convert it row by row into XML, as so:

    import pandas as pd
    df = pd.read_csv('untitled.txt', sep='|')
    

    With the sample data (assuming separator and so on) loaded as:

              Title                   Type Format  Year Rating  Stars  \
    0  Enemy Behind           War,Thriller    DVD  2003     PG     10   
    1  Transformers  Anime,Science Fiction    DVD  1989      R      9   
    
                 Description  
    0          Talk about...  
    1  A Schientific fiction  
    

    And then converting to xml with a custom function:

    def convert_row(row):
        return """
        %s
        %s
        %s
        %s
        %s
        %s
    """ % (
        row.Title, row.Type, row.Format, row.Year, row.Rating, row.Stars, row.Description)
    
    print '\n'.join(df.apply(convert_row, axis=1))
    

    This way you get a string containing the xml:

    
        War,Thriller
        DVD
        2003
        PG
        10
        Talk about...
    
    
        Anime,Science Fiction
        DVD
        1989
        R
        9
        A Schientific fiction
    
    

    that you can dump in to a file or whatever.

    Inspired by this great answer.


    Edit: Using the loading method you posted (or a version that actually loads the data to a variable):

    import csv              
    f = open('movies2.csv')
    csv_f = csv.reader(f)   
    data = []
    
    for row in csv_f: 
       data.append(row)
    f.close()
    
    print data[1:]
    

    We get:

    [['Enemy Behind', 'War', 'Thriller', 'DVD', '2003', 'PG', '10', 'Talk about...'], ['Transformers', 'Anime', 'Science Fiction', 'DVD', '1989', 'R', '9', 'A Schientific fiction']]
    

    And we can convert to XML with minor modifications:

    def convert_row(row):
        return """
        %s
        %s
        %s
        %s
        %s
        %s
    """ % (row[0], row[1], row[2], row[3], row[4], row[5], row[6])
    
    print '\n'.join([convert_row(row) for row in data[1:]])
    

    Getting identical results:

    
        War
        Thriller
        DVD
        2003
        PG
        10
    
    
        Anime
        Science Fiction
        DVD
        1989
        R
        9
    
    

    0 讨论(0)
提交回复
热议问题