简单的CSV到XML转换 - Python

时间:2016-12-09 11:19:56

标签: python xml csv parsing

我正在寻找一种自动将CSV转换为XML的方法。

以下是CSV文件的示例,其中包含电影列表:

Movies Csv

以下是XML格式的文件:

import csv              
f = open('movies2.csv')
csv_f = csv.reader(f)   

def convert_row(row):
   return """<movietitle="%s">
   <type>%s</type>
   <format>%s</format>
   <year>%s</year>
   <rating>%s</rating>
   <stars>%s</stars>
   <description>%s</description>
</movie>""" % (
   row.Title, row.Type, row.Format, row.Year, row.Rating, row.Stars, row.Description)

print ('\n'.join(csv_f.apply(convert_row, axis=1)))

我已经尝试了几个例子,我可以使用DOM和SAX使用Python读取csv和XML格式,但我找到了一个简单的转换示例。到目前为止,我有:

 File "moviesxml.py", line 16, in module
   print ('\n'.join(csv_f.apply(convert_row, axis=1)))
AttributeError: '_csv.reader' object has no attribute 'apply'

但我收到错误:

public void actionPerformed(ActionEvent ae)
{
    String s=ae.getActionCommand();
    double salary=Integer.parseInt(t12.getText());

    if(s.equals("Clothes"))
    {
        int i=Integer.parseInt(amount.getText());
        sum1=i+sum1;
        t1.setText("You spend "+Integer.toString(sum1)+" in Clothes");
        w2.t1.setText(Integer.toString(sum1));
        per=(sum1/salary)*100;
        S.t11.setText(Double.toString(per));     
    }
    else if(s.equals("Food"))
    {
        int i=Integer.parseInt(amount.getText());
        sum2=i+sum2;
        t1.setText("You spend "+Integer.toString(sum2)+" in Food");
        w2.t2.setText(Integer.toString(sum2));
        per=(sum2/salary)*100;
        S.t22.setText(Double.toString(per));
    }
    else if(s.equals("Bills"))
    {
        int i=Integer.parseInt(amount.getText());
        sum3=i+sum3;
        t1.setText("You spend "+Integer.toString(sum3)+" in Bills");
        w2.t3.setText(Integer.toString(sum3));
        per=(sum3/salary)*100;
        S.t33.setText(Double.toString(per));
    }
    else if(s.equals("Transportation"))
    {
        int i=Integer.parseInt(amount.getText());
        sum4=i+sum4;
        t1.setText("You spend "+Integer.toString(sum4)+" in Transportation");
        w2.t4.setText(Integer.toString(sum4));
        per=(sum4/salary)*100;
        S.t44.setText(Double.toString(per));
    }
    else if(s.equals("Others"))
    {
        int i=Integer.parseInt(amount.getText());
        sum5=i+sum5;
        t1.setText("You spend "+Integer.toString(sum5)+" in Others");
        w2.t5.setText(Integer.toString(sum5));
        per=(sum5/salary)*100;
        S.t55.setText(Double.toString(per));
    }
    else if(s.equals("Next"))
    {
        t1.setText(null);
        amount.setText(null);
        t13.setText(Double.toString(salary-(sum1+sum2+sum3+sum4+sum5)));       
    }
    else if(s.equals("Check"))
    {
        w2.setVisible(true);
        w2.setSize(600,400);  
    }
    else
    System.exit(0);

我对Python很陌生,所以任何帮助都会非常感激!

我使用的是Python 3.5.2。

谢谢!

莉莎

1 个答案:

答案 0 :(得分:9)

一种可能的解决方案是首先将csv加载到Pandas中,然后逐行将其转换为XML,如下所示:

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

将样本数据(假设分隔符等)加载为:

          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  

然后使用自定义函数转换为xml:

def convert_row(row):
    return """<movietitle="%s">
    <type>%s</type>
    <format>%s</format>
    <year>%s</year>
    <rating>%s</rating>
    <stars>%s</stars>
    <description>%s</description>
</movie>""" % (
    row.Title, row.Type, row.Format, row.Year, row.Rating, row.Stars, row.Description)

print '\n'.join(df.apply(convert_row, axis=1))

这样你就得到一个包含xml的字符串:

<movietitle="Enemy Behind">
    <type>War,Thriller</type>
    <format>DVD</format>
    <year>2003</year>
    <rating>PG</rating>
    <stars>10</stars>
    <description>Talk about...</description>
</movie>
<movietitle="Transformers">
    <type>Anime,Science Fiction</type>
    <format>DVD</format>
    <year>1989</year>
    <rating>R</rating>
    <stars>9</stars>
    <description>A Schientific fiction</description>
</movie>

您可以转储到文件或其他任何内容。

this great answer.启发

编辑:使用您发布的加载方法(或实际将数据加载到变量的版本):

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:]

我们得到:

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

我们可以通过微小的修改转换为XML:

def convert_row(row):
    return """<movietitle="%s">
    <type>%s</type>
    <format>%s</format>
    <year>%s</year>
    <rating>%s</rating>
    <stars>%s</stars>
    <description>%s</description>
</movie>""" % (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:]])

获得相同的结果:

<movietitle="Enemy Behind">
    <type>War</type>
    <format>Thriller</format>
    <year>DVD</year>
    <rating>2003</rating>
    <stars>PG</stars>
    <description>10</description>
</movie>
<movietitle="Transformers">
    <type>Anime</type>
    <format>Science Fiction</format>
    <year>DVD</year>
    <rating>1989</rating>
    <stars>R</stars>
    <description>9</description>
</movie>