将XML上的逗号替换为CSV分析器 [英] Replacing comma on XML to CSV Parser

查看:139
本文介绍了将XML上的逗号替换为CSV分析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将只包含我尝试实现新功能的部分代码

I’m only going to include the part of the code that I’m trying to implement a new feature

            for counter, st in enumerate( sn.findall('.//SetData') ):
                for k,v in st.attrib.items():
                    if v.startswith("-"):      #If a string startes with - it removes it
                        v = v.replace("-","",1)
                    row['set_data_'+ str(k)] = v
                row["Counter"] = counter        #just a counter in another colummn
                row_data = [row[i] for i in headers]
                writer.writerow(row_data)
                row = defaultdict(str)

要解析的元素之一具有十进制数字,这些数字用逗号而不是点分隔,从而使它们成为字符串

One of the elements that is being parsed has decimals numbers that are separated by a comma and not a dot and thus making them a string

因此,当解析元素时,我希望像这样55,4而不是55.4

So instead when the element is being parsed I wanted the values to be parsed like this 55,4 instead 55.4

当数字之间用点分隔时,它们仍然是整数,这使得进一步使用变得更容易.

When the numbers are separated by dot they are still integers which makes it easier for further use.

推荐答案

,您可以根据您的语言环境设置来解析数字.

you can parse the number according to you locale settings.

import locale

locale.setlocale(locale.LC_ALL, 'danish')

s = '1.237,5'
locale.atof(s)

这会将字符串s解析为具有值1237.5

this will parse the string s to a float with value 1237.5

要列出可用的语言环境,请使用:locale.locale_alias

To list available locales use: locale.locale_alias

这篇关于将XML上的逗号替换为CSV分析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆