用python更新字符串中数字精度(保留位数)

tech2023-09-04  90

记录一次数据处理过程,对坐标点集进行简化,即对坐标点集精度大于小数点后6位的阶段到6位

import re # 将小数点后,位数大于i位的截断到第i位 def truncString(string, i): if ('.' in string) and string.index('.') > 0: string = string[0:(string.index('.')) + i + 1] else: string = string return string # finalString('POLYGON',k),传入字符串和精度k,返回精度为k位的字符串 def finalString(inputString, k): outString = [] finalString = '' # 提取数字生成数组 num = re.findall('\d+(?:\.\d+)?', inputString) # print(num) remain = inputString; for i in range(0, len(num)): # 获取第一个数字长度 len1 = len(num[i]) # 获取第一个数字的值 num[i] = truncString(num[i], k) # print(num[i]) # 获取第一个数字的位置 loc = remain.index(num[i]) # 截取第一段字符串 c = remain[0:loc] outString.append(c) # print(outString[i]) # 拼接第一段字符串 finalString += c + num[i] # print(finalString) # 截取剩余字符串 remain = remain[loc + len1:] # print(remain) finalString += remain return finalString outputString = finalString( 'MULTIPOLYGON(((117.79786 38.2127330000001,117.796298509813 38.2122540627565,117.797994929247 38.2146177929214,117.79786 38.2127330000001)),((117.804646 38.227125,117.80421887456 38.2270973949481,117.805232189796 38.2277105001606,117.804646 38.227125)),((117.207257 40.087458,117.170517 39.641574,116.9703 39.6353170000001,116.770348 40.023506,117.207257 40.087458)),((117.80003 42.6148020000001,118.291322 42.0536680000001,118.349755 41.3292780000001,119.233212 41.3100359999999,118.842294 40.8048970000001,119.560414 40.5317119999999,119.84002 39.985634,119.011207 39.203618,118.204616 39.107786,117.532452 39.7586870000001,117.769013 40.059331,117.217202 40.374115,117.499237 40.6658930000001,116.334755 40.9292560000001,116.459271 40.7709690000001,115.748253 40.5386380000001,115.962158 40.2626169999999,115.446255 40.0218310000001,115.472203 39.6491770000001,116.8065 39.6147070000001,116.739331 38.752734,117.547375619479 38.6074155379453,117.797215 38.172235,115.97028 37.336239,115.288361 36.519155,115.477513 36.148807,113.731622 36.356224,113.45559 36.7113830000001,114.133496 37.6912929999999,113.548325 38.5562249999999,114.556493 39.5559780000001,113.903031 40.008545,114.527333 40.340937,113.820188 41.0918180000001,113.869114 41.4418900000001,114.238484 41.5172910000001,114.512123 42.1181070000001,114.820824 42.1457510000001,114.861039 41.600049,115.232941 41.573887,115.831635 41.9351090000001,116.710389 41.939213,116.908582 42.399801,117.80003 42.6148020000001),(116.696995 37.733294,116.69269 37.731407,116.70472 37.736679,116.696995 37.733294),(115.46975 36.755634,115.4694 36.753348,115.46904 36.750992,115.46975 36.755634),(115.388380847522 36.6609794602318,115.388007021834 36.6608276065503,115.388145 36.660869,115.388380847522 36.6609794602318)))' , 2) print(outputString) # 打印结果: MULTIPOLYGON(((117.79 38.21,117.79 38.21,117.79 38.21,117.79 38.21)),((117.80 38.22,117.80 38.22,117.80 38.22,117.80 38.22)),((117.20 40.08,117.17 39.64,116.97 39.63,116.77 40.02,117.20 40.08)),((117.80 42.61,118.29 42.05,118.34 41.32,119.23 41.31,118.84 40.80,119.56 40.53,119.84 39.98,119.01 39.20,118.20 39.10,117.53 39.75,117.76 40.05,117.21 40.37,117.49 40.66,116.33 40.92,116.45 40.77,115.74 40.53,115.96 40.26,115.44 40.02,115.47 39.64,116.80 39.61,116.73 38.75,117.54 38.60,117.79 38.17,115.97 37.33,115.28 36.51,115.47 36.14,113.73 36.35,113.45 36.71,114.13 37.69,113.54 38.55,114.55 39.55,113.90 40.00,114.52 40.34,113.82 41.09,113.86 41.44,114.23 41.51,114.51 42.11,114.82 42.14,114.86 41.60,115.23 41.57,115.83 41.93,116.71 41.93,116.90 42.39,117.80 42.61),(116.69 37.73,116.69 37.73,116.70 37.73,116.69 37.73),(115.46 36.75,115.46 36.75,115.46 36.75,115.46 36.75),(115.38 36.66,115.38 36.66,115.38 36.66,115.38 36.66))) # 连接数据库对字段进行更新 # 表名 tablename = 'chinaprovinceboundary' # 精度 k = 6 # 连接到一个给定的数据库 conn = psycopg2.connect(database="postgres", user="postgres", password="postgres", host="192.168.X.X", port="5432") # 建立游标,用来执行数据库操作 cursor = conn.cursor() # 执行SQL SELECT命令 cursor.execute("select * from " + tablename) result = [] for i in range(cursor.rowcount + 1): result.append([]) # 获取SELECT返回的元组 for i in range(cursor.rowcount): rows = cursor.fetchone() result[rows[0]] = finalString(rows[7], k) print(result[rows[0]]) for i in range(1, len(result)): cursor.execute("update " + tablename + " set poly_0903 = '" + str(result[i]) + "' where gid = " + str(i)) conn.commit() # 关闭游标 cursor.close() # 关闭数据库连接 conn.close()
最新回复(0)