目录
1.按照某个固定大小将一个csv文件切分成多个:
2.将csv文件写到xml中(将查找插入位置的代码放到for循环外部,插入的速度会快一点):
f = open('data_value(1).csv', encoding='utf-8')
l = []
head = f.readline()
l.append(head)
for fin in f:
l.append(fin)
for i in range(186):
filename = str(i) + '.csv'
fout = open(filename, 'w', encoding='utf-8')
for j in range(151):
fout.write(l[i*151:(i+1)*151][j])
from xml.etree import ElementTree as ET
from xml.etree.ElementTree import Element, SubElement
import pandas as pd
import numpy as np
import time
t1 = time.time()
# 要写入的数据1
data = pd.read_csv('coor_16.csv', header=None)
data = np.array(data)
# 要写入的数据2
data1 = pd.read_csv('16.csv', header=None)
data1 = np.array(data1)
data2 = np.transpose(data1)
tree = ET.ElementTree(file='stdData0_1.xml')
ele = tree.find(要插入的指定位置1)
ele2 = tree.find(要插入的指定位置2)
for i in range(1674):
# 创建节点的标签,以及需要添加在标签中的内容
feature = SubElement(ele, 'feature')
feature.set("geoType", "1")
CPoints = SubElement(feature, 'CPoints')
coordinate = SubElement(CPoints, 'coordinate')
x = data[i][1]
y = data[i][2]
x_str = str(x)
y_str = str(y)
coordinate.text = x_str +' ' + y_str + ' ' + str(0.000000)
coordinate.tail = "\n"
CRecord = SubElement(feature, 'CRecord')
Drilling_code = SubElement(CRecord, "钻孔编码")
code = data[i][0]
code_str = str(int(code))
Drilling_code.text = 'Trace_' + code_str
Drilling_code.tail = "\n"
Drilling_depth = SubElement(CRecord, "钻孔埋深")
Drilling_depth.text = '9000'
Drilling_depth.tail = "\n"
Drilling_elevation = SubElement(CRecord, "钻孔标高")
Drilling_elevation.text = '0'
Drilling_elevation.tail = "\n"
Drilling_type = SubElement(CRecord, "钻孔类型")
Drilling_type.text = '1'
Drilling_type.tail = "\n"
Reliability = SubElement(CRecord, "可靠性")
Reliability.text = '1'
Reliability.tail = "\n"
Remarks = SubElement(CRecord, "备注")
Remarks.text = 'afaf'
Remarks.tail = "\n"
Pnt_info = SubElement(feature, "PNT_INFO")
libID = SubElement(Pnt_info, "libID")
libID.text = '0'
libID.tail ='\n'
symID = SubElement(Pnt_info, "symID")
symID.text = '1'
symID.tail = '\n'
height = SubElement(Pnt_info, "height")
height.text = '10.000000'
height.tail ='\n'
width = SubElement(Pnt_info, "width")
width.text = '10.000000'
width.tail = '\n'
angle = SubElement(Pnt_info, "angle")
angle.text = '0.000'
angle.tail = '\n'
outpenw = SubElement(Pnt_info, "outpenw")
outpenw.text = '0.050000 0.050000 0.050000'
outpenw.tail = '\n'
outclr = SubElement(Pnt_info, "outclr")
outclr.text = '5 4 3'
outclr.tail = '\n'
ovprnt = SubElement(Pnt_info, "ovprnt")
ovprnt.text = '0'
ovprnt.tail = '\n'
fillflg = SubElement(Pnt_info, "fillflg")
fillflg.text = '1'
fillflg.tail = '\n'
backexp = SubElement(Pnt_info, "backexp")
backexp.text = '1.000000'
backexp.tail = '\n'
backclr = SubElement(Pnt_info, "backclr")
backclr.text = '1'
backclr.tail = '\n'
for m in range (1674):
for n in range (150):
print("现在进行第" + str(m) + "列第" + str(n) + "行的数据")
feature2 = SubElement(ele2, 'feature')
feature2.set("geoType", "4")
CAnyLine = SubElement(feature2, 'CAnyLine')
coordinate2 = SubElement(CAnyLine, 'coordinate')
# x2 = data[m][1]
# y2 = data[m][2]
# x_str2 = str(x2)
# y_str2 = str(y2)
coordinate2.text = '0.000000 0.000000 0.000000'
coordinate2.tail = "\n"
CRecord2 = SubElement(feature2, 'CRecord')
Drilling_code2 = SubElement(CRecord2, "钻孔编码")
code2 = data[m][0]
code2_str = str(int(code2))
Drilling_code2.text = 'Trace_' + code2_str
Drilling_code2.tail = "\n"
Hierarchical_coding = SubElement(CRecord2, "分层编码")
coding_data = data1[n][m]
coding_data_str = str(coding_data)
Hierarchical_coding.text = coding_data_str
Hierarchical_coding.tail = "\n"
Hierarchical_level = SubElement(CRecord2, "分层级别")
Hierarchical_level.text = Hierarchical_coding.text
Hierarchical_level.tail = "\n"
Hierarchical_id = SubElement(CRecord2, "分层名称")
Hierarchical_id.text = Hierarchical_coding.text
Hierarchical_id.tail = "\n"
Top_depth = SubElement(CRecord2, "层顶埋深")
top_data = 60*n
top_str = str(top_data)
Top_depth.text = top_str
Top_depth.tail = "\n"
Bottom_depth = SubElement(CRecord2, "层底埋深")
Bottom_data = 60 * (n+1)
Bottom_str = str(Bottom_data)
Bottom_depth.text = Bottom_str
Bottom_depth.tail = "\n"
Zenith_angle = SubElement(CRecord2, "天顶角")
Zenith_angle.text = '0'
Zenith_angle.tail = "\n"
Azimuth = SubElement(CRecord2, "方位角")
Azimuth.text = '0'
Azimuth.tail = "\n"
Hierarchical_description = SubElement(CRecord2, "分层描述")
Hierarchical_description.text = '0'
Hierarchical_description.tail = "\n"
Fill_pattern = SubElement(CRecord2, "填充图案")
Fill_pattern.text = '0'
Fill_pattern.tail = "\n"
Background = SubElement(CRecord2, "背景色")
Background.text = '0'
Background.tail = "\n"
mpLength = SubElement(CRecord2, 'mpLength')
mpLength.text = '0'
mpLength.tail = "\n"
Lin_info = SubElement(feature2, "LIN_INFO")
libID = SubElement(Lin_info, "libID")
libID.text = '0'
libID.tail = '\n'
linstyID = SubElement(Lin_info, "linstyID ")
linstyID .text = '1'
linstyID .tail = '\n'
ovprnt = SubElement(Lin_info, "ovprnt")
ovprnt.text = '0'
ovprnt.tail = '\n'
outpenw = SubElement(Lin_info, "outpenw")
outpenw.text = '0.050000 0.050000 0.050000'
outpenw.tail = '\n'
outclr = SubElement(Lin_info, "outclr")
outclr.text = '1 1 1'
outclr.tail = '\n'
headtype = SubElement(Lin_info, "headtype")
headtype.text = '0'
headtype.tail = '\n'
jointype = SubElement(Lin_info, "jointype")
jointype.text = '0'
jointype.tail = '\n'
adjustflg = SubElement(Lin_info, "adjustflg")
adjustflg.text = '0'
adjustflg.tail = '\n'
makemethod = SubElement(Lin_info, "makemethod")
makemethod.text = '0'
makemethod.tail = '\n'
xscale = SubElement(Lin_info, "xscale")
xscale.text = '10.000000'
xscale.tail = '\n'
yscale = SubElement(Lin_info, "yscale")
yscale.text = '0.000'
yscale.tail = '\n'
tree.write('result_last.xml', encoding="utf-8")
t2 = time.time()
print('time cost: %.6f' % (t2 - t1))