odoo 瀵煎叆鏂囦欢xml鍒朵綔,浣跨敤csv 鏂囦欢鍒朵綔xml 鏂囦欢
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了odoo 瀵煎叆鏂囦欢xml鍒朵綔,浣跨敤csv 鏂囦欢鍒朵綔xml 鏂囦欢相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/rand' title='rand'>rand
str 鐢熸垚 with open enumerate range 鏂囦欢 鏂瑰紡 newimport csv
import random
class XmlFormat(object):
def __init__(self):
self.wf = open(鈥榙ata/res_location.xml鈥? 鈥榳鈥? encoding=鈥榰tf-8鈥?
self.add_header()
def add_header(self):
header = """<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="1">
"""
# print(header)
self.wf.write(header)
def add_tail(self):
tail = """
</data>
</odoo>
"""
self.wf.write(tail)
def get_random(self):
return random.randrange(1000, 9999, 1)
def readfile(self):
with open(鈥榙ata/location.csv鈥? 鈥榬鈥? encoding=鈥榰tf-8鈥? as f:
reder = csv.reader(f, delimiter=鈥?鈥?
for i, line in enumerate(reder):
if not line:
continue
# todo: 鑷姩鎶撳彇琛ㄥご淇℃伅,骞惰缃敭鍊煎:
if i == 0:
# 瀛樺偍琛ㄥご瀛楁
if not line:
raise
header_line = line
continue
data = {}
# print(line)
# 澶勭悊姣忎竴琛岀殑鏁版嵁
for e, var in enumerate(line):
# print(e,var,header_line[e])
if header_line[e] == 鈥榥ew-code鈥?
data[header_line[e]] = var or self.get_random()
elif header_line[e] == 鈥榠s_warehouse鈥?
data[header_line[e]] = var or 0
else:
data[header_line[e]] = var
if (i) % 1000 == 0:
# 鍏抽棴涔嬪墠鐨勬枃浠? self.add_tail()
self.wf.close()
self.wf = open(鈥榙ata/res_location.xml鈥?format(i), 鈥榳鈥? encoding=鈥榰tf-8鈥?
print(鈥榎鈥榙ata/res_location{}.xml鈥?鈥?format(i))
self.add_header()
# 杩欓噷浼犻€掑弬鏁扮殑鏂瑰紡鍙互浣跨敤**kw
self.generate_xml_code(**data)
def generate_xml_code(self, **kwargs):
"""
璁剧疆xml 妯℃澘,骞剁敓鎴愮浉搴旂殑xml鏂囨湰,
澶ф嫭鍙蜂腑鐨勫弬鏁伴渶瑕佸拰csv 鏂囦欢鐨勫瓧娈典俊鎭繚鎸佷竴鑷?濡倇extra_id} 瀵瑰簲csv 鏂囦欢涓殑extra_id瀛楁
xml 妯℃澘鎸夌収鍙寜鐓ц嚜宸辩殑闇€姹傝繘琛屾洿鎹?
娉? csv 鏂囦欢鐨勫瓧娈典笉鑳介噸澶?
:param kwargs:
:return:
"""
str_format = """
<record id="{extra_id}" model="stock.location">
<field name="name">{name}</field>
<field name="usage">internal</field>
<field name="is_warehouse">{is_warehouse}</field>
<field name="active">1</field>
<field name="company_id" ref="base.main_company"/>
<field name="location_id" ref="{extra_id_parent}"/>
<field name="barcode">{new-code}</field>
</record>
"""
res = str_format.format(**kwargs)
self.wf.write(res)
def __del__(self):
self.add_tail()
self.wf.close()
if __name__ == 鈥榑_main__鈥?
a = XmlFormat()
a.readfile()
璇存槑:
- csv鏂囦欢闇€瑕佹斁鍦╠ata鏂囦欢澶逛腑,鍏跺疄鍙互淇敼璇诲彇鏂囦欢鐨勪綅缃?/li>
以上是关于odoo 瀵煎叆鏂囦欢xml鍒朵綔,浣跨敤csv 鏂囦欢鍒朵綔xml 鏂囦欢的主要内容,如果未能解决你的问题,请参考以下文章
Linux(CentOS)浣跨敤瀹氭椂浠诲姟鍒犻櫎Tomcat鏃ュ織鏂囦欢