代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Python
】
把CSV文件转换成XML文件
作者:
/ 发布于
2011/1/4
/
593
<div> <table style="font-family: monospace" class="python codes"> <tbody> <tr class="li1"> <td style="line-height: 150%; font-family: Verdana, Monospace; font-size: 12px; font-weight: bold; margin-right: 10px"> <pre style="line-height: 150%; font-family: Verdana, Monospace; font-size: 12px; font-weight: bold; margin-right: 10px"><span style="font-style: italic; color: #808080"># csv2xml.py</span> <span style="font-style: italic; color: #808080"># FB - 201010107</span> <span style="font-style: italic; color: #808080"># First row of the csv file must be header!</span> <span style="font-style: italic; color: #808080"># example CSV file: myData.csv</span> <span style="font-style: italic; color: #808080"># id,code name,value</span> <span style="font-style: italic; color: #808080"># 36,abc,7.6</span> <span style="font-style: italic; color: #808080"># 40,def,3.6</span> <span style="font-style: italic; color: #808080"># 9,ghi,6.3</span> <span style="font-style: italic; color: #808080"># 76,def,99</span> <span style="color: #0099cc">import</span> <span style="color: blue">csv</span> csvFile = <span style="color: #483d8b">'myData.csv'</span> xmlFile = <span style="color: #483d8b">'myData.xml'</span> csvData = <span style="color: blue">csv</span>.<span style="color: black">reader</span><span style="color: black">(</span><span style="color: #0000ff">open</span><span style="color: black">(</span>csvFile<span style="color: black">)</span><span style="color: black">)</span> xmlData = <span style="color: #0000ff">open</span><span style="color: black">(</span>xmlFile, <span style="color: #483d8b">'w'</span><span style="color: black">)</span> xmlData.<span style="color: black">write</span><span style="color: black">(</span><span style="color: #483d8b">'<?xml version="1.0"?>'</span> + <span style="color: #483d8b">"<span style="color: #000099; font-weight: bold">\n</span>"</span><span style="color: black">)</span> <span style="font-style: italic; color: #808080"># there must be only one top-level tag</span> xmlData.<span style="color: black">write</span><span style="color: black">(</span><span style="color: #483d8b">'<csv_data>'</span> + <span style="color: #483d8b">"<span style="color: #000099; font-weight: bold">\n</span>"</span><span style="color: black">)</span> rowNum = <span style="color: #ff4500">0</span> <span style="color: #0099cc">for</span> row <span style="color: #0099cc">in</span> csvData: <span style="color: #0099cc">if</span> rowNum == <span style="color: #ff4500">0</span>: tags = row <span style="font-style: italic; color: #808080"># replace spaces w/ underscores in tag names</span> <span style="color: #0099cc">for</span> i <span style="color: #0099cc">in</span> <span style="color: #0000ff">range</span><span style="color: black">(</span><span style="color: #0000ff">len</span><span style="color: black">(</span>tags<span style="color: black">)</span><span style="color: black">)</span>: tags<span style="color: black">[</span>i<span style="color: black">]</span> = tags<span style="color: black">[</span>i<span style="color: black">]</span>.<span style="color: black">replace</span><span style="color: black">(</span><span style="color: #483d8b">' '</span>, <span style="color: #483d8b">'_'</span><span style="color: black">)</span> <span style="color: #0099cc">else</span>: xmlData.<span style="color: black">write</span><span style="color: black">(</span><span style="color: #483d8b">'<row>'</span> + <span style="color: #483d8b">"<span style="color: #000099; font-weight: bold">\n</span>"</span><span style="color: black">)</span> <span style="color: #0099cc">for</span> i <span style="color: #0099cc">in</span> <span style="color: #0000ff">range</span><span style="color: black">(</span><span style="color: #0000ff">len</span><span style="color: black">(</span>tags<span style="color: black">)</span><span style="color: black">)</span>: xmlData.<span style="color: black">write</span><span style="color: black">(</span><span style="color: #483d8b">' '</span> + <span style="color: #483d8b">'<'</span> + tags<span style="color: black">[</span>i<span style="color: black">]</span> + <span style="color: #483d8b">'>'</span> \ + row<span style="color: black">[</span>i<span style="color: black">]</span> + <span style="color: #483d8b">'</'</span> + tags<span style="color: black">[</span>i<span style="color: black">]</span> + <span style="color: #483d8b">'>'</span> + <span style="color: #483d8b">"<span style="color: #000099; font-weight: bold">\n</span>"</span><span style="color: black">)</span> xmlData.<span style="color: black">write</span><span style="color: black">(</span><span style="color: #483d8b">'</row>'</span> + <span style="color: #483d8b">"<span style="color: #000099; font-weight: bold">\n</span>"</span><span style="color: black">)</span> rowNum +=<span style="color: #ff4500">1</span> xmlData.<span style="color: black">write</span><span style="color: black">(</span><span style="color: #483d8b">'</csv_data>'</span> + <span style="color: #483d8b">"<span style="color: #000099; font-weight: bold">\n</span>"</span><span style="color: black">)</span> xmlData.<span style="color: black">close</span><span style="color: black">(</span><span style="color: black">)</span></pre> </td> </tr> </tbody> </table> </div>
试试其它关键字
同语言下
.
比较两个图片的相似度
.
过urllib2获取带有中文参数的url内容
.
不下载获取远程图片的宽度和高度及文件大小
.
通过qrcode库生成二维码
.
通过httplib发送GET和POST请求
.
Django下解决小文件下载
.
遍历windows的所有窗口并输出窗口标题
.
根据窗口标题调用窗口
.
python 抓取搜狗指定公众号
.
pandas读取指定列
可能有用的
.
比较两个图片的相似度
.
过urllib2获取带有中文参数的url内容
.
不下载获取远程图片的宽度和高度及文件大小
.
通过qrcode库生成二维码
.
通过httplib发送GET和POST请求
.
Django下解决小文件下载
.
遍历windows的所有窗口并输出窗口标题
.
根据窗口标题调用窗口
.
python 抓取搜狗指定公众号
.
pandas读取指定列
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3