自学内容网 自学内容网

Excel文件转Asc文件

单个转换

import os
import pandas as pd

filename = '(10)result01-1.xlsx'
df = pd.read_excel(filename)  # 读取Excel文件

# 将数据保存为ASC格式
asc_filename = os.path.splitext(filename)[0] + '.asc'  # 获取文件名并替换扩展名
with open(asc_filename, 'w') as file:
    # 写入文件头
    file.write('NCOLS {}\n'.format(df.shape[1]))  # 列数
    file.write('NROWS {}\n'.format(df.shape[0]))  # 行数
    file.write('XLLCENTER {}\n'.format(0))  # X轴坐标起始值
    file.write('YLLCENTER {}\n'.format(0))  # Y轴坐标起始值
    file.write('CELLSIZE {}\n'.format(1))  # 单元格大小
    file.write('NODATA_VALUE {}\n'.format(-9999))  # 无效值

    # 写入数据
    for i in range(df.shape[0]):
        row = df.iloc[i]
        row_str = ' '.join([str(x) for x in row.values])
        file.write(row_str + '\n')
 批量转换(指定目录批量转换,且转换文件也在指定目录下)
import os
import pandas as pd

folder_path = 'F:/data'  # 文件夹路径
for filename in os.listdir(folder_path):
    if filename.endswith('.xlsx'):
        # 读取Excel文件
        df = pd.read_excel(os.path.join(folder_path, filename))

        # 将数据保存为ASC格式
        asc_filename = os.path.splitext(filename)[0] + '.asc'  # 获取文件名并替换扩展名
        asc_path = os.path.join(folder_path, asc_filename)  # 获取保存ASC文件的路径
        with open(asc_path, 'w') as file:
            # 写入文件头
            file.write('NCOLS {}\n'.format(df.shape[1]))  # 列数
            file.write('NROWS {}\n'.format(df.shape[0]))  # 行数
            file.write('XLLCENTER {}\n'.format(0))  # X轴坐标起始值
            file.write('YLLCENTER {}\n'.format(0))  # Y轴坐标起始值
            file.write('CELLSIZE {}\n'.format(1))  # 单元格大小
            file.write('NODATA_VALUE {}\n'.format(-9999))  # 无效值

            # 写入数据
            for i in range(df.shape[0]):
                row = df.iloc[i]
                row_str = ' '.join([str(x) for x in row.values])
                file.write(row_str + '\n')

注意:

如果转换后不想要 以下内容

NCOLS 3
NROWS 169
XLLCENTER 0
YLLCENTER 0
CELLSIZE 1
NODATA_VALUE -9999

直接注释代码中的以下内容

file.write('NCOLS {}\n'.format(df.shape[1]))  # 列数
file.write('NROWS {}\n'.format(df.shape[0]))  # 行数
file.write('XLLCENTER {}\n'.format(0))  # X轴坐标起始值
file.write('YLLCENTER {}\n'.format(0))  # Y轴坐标起始值
file.write('CELLSIZE {}\n'.format(1))  # 单元格大小
file.write('NODATA_VALUE {}\n'.format(-9999))  # 无效值

转换前后(前面是excel数据),后面是asc文件数据

xyzNCOLS 3
0.000.004836.00NROWS 169
-427.850.004890.32XLLCENTER 0
-133.86-411.964951.09YLLCENTER 0
348.46-253.174923.19CELLSIZE 1
343.03249.234846.49NODATA_VALUE -9999
-287.29884.1910626.410.0 0.0 4836.0
-1503.881092.6410542.37-427.85 0.0 4890.32
-259.72799.324766.47-133.86 -411.96 4951.09
257.84793.544732.00348.46 -253.17 4923.19
1523.831107.1310682.21343.03 249.23 4846.49
864.250.004901.39-287.29 884.19 10626.41
700.88-509.224913.21-1503.88 1092.64 10542.37
266.53-820.304891.54-259.72 799.32 4766.47
-269.32-828.884942.75257.84 793.54 4732.0
-703.41-511.054930.931523.83 1107.13 10682.21
-869.460.004930.93864.25 0.0 4901.39
-1223.640.004907.76700.88 -509.22 4913.21
-1078.98-622.954997.02266.53 -820.3 4891.54
-619.08-1072.274965.97-269.32 -828.88 4942.75
0.00-1244.934993.14-703.41 -511.05 4930.93
618.59-1071.444962.09-869.46 0.0 4930.93
1070.81-618.234959.18-1223.64 0.0 4907.76
1206.220.004837.89-1078.98 -622.95 4997.02
1050.49606.504865.06-619.08 -1072.27 4965.97
513.96890.214122.790.0 -1244.93 4993.14
0.00848.903404.77618.59 -1071.44 4962.09
-432.19748.583466.871070.81 -618.23 4959.18
-1051.53607.104869.911206.22 0.0 4837.89
-1443.41642.654862.751050.49 606.5 4865.06
-684.00759.663146.09513.96 890.21 4122.79
-222.30684.182214.060.0 848.9 3404.77
79.69758.172346.26-432.19 748.58 3466.87
435.87754.952682.93-1051.53 607.1 4869.91
1264.00918.354808.54-1443.41 642.65 4862.75
1408.25299.334430.97-684.0 759.66 3146.09
1424.87-302.874483.28-222.3 684.18 2214.06
1272.25-924.344839.9379.69 758.17 2346.26
816.11-1413.555023.48435.87 754.95 2682.93
170.45-1621.755018.731264.0 918.35 4808.54
-499.23-1536.474972.121408.25 299.33 4430.97
-1088.45-1208.855006.361424.87 -302.87 4483.28
-1447.64-644.534877.021272.25 -924.34 4839.93
-1511.090.004650.67816.11 -1413.55 5023.48
-1682.360.004163.98170.45 -1621.75 5018.73
-1561.19-568.234112.06-499.23 -1536.47 4972.12
-1389.77-1166.164490.35-1088.45 -1208.85 5006.36
-1022.11-1770.355059.64-1447.64 -644.53 4877.02
-356.02-2019.075074.48-1511.09 0.0 4650.67
353.61-2005.425040.17-1682.36 0.0 4163.98
967.42-1675.624788.90-1561.19 -568.23 4112.06
1443.15-1210.954662.81-1389.77 -1166.16 4490.35
1538.31-559.904051.79-1022.11 -1770.35 5059.64
1480.820.003665.16-356.02 -2019.07 5074.48
1602.72583.344221.47353.61 -2005.42 5040.17
1087.89912.843514.95967.42 -1675.62 4788.9
511.15885.342530.281443.15 -1210.95 4662.81
141.55802.762017.551538.31 -559.9 4051.79
-136.08771.771939.671480.82 0.0 3665.16
-409.82709.832028.681602.72 583.34 4221.47
-778.82653.512516.381087.89 912.84 3514.95
-1771.69644.844666.52511.15 885.34 2530.28
-1691.84549.713647.31141.55 802.76 2017.55
-1054.02765.792671.22-136.08 771.77 1939.67
-556.05765.331939.60-409.82 709.83 2028.68
-258.06794.221712.20-778.82 653.51 2516.38
0.00768.461575.59-1771.69 644.84 4666.52
252.10775.881672.66-1691.84 549.71 3647.31
570.22784.841989.03-1054.02 765.79 2671.22
1316.46956.463336.32-556.05 765.33 1939.6
1493.81485.373220.38-258.06 794.22 1712.2
1524.220.003125.110.0 768.46 1575.59
1489.22-483.883210.49252.1 775.88 1672.66
1498.40-1088.653797.40570.22 784.84 1989.03
1430.06-1968.314988.311316.46 956.46 3336.32
758.73-2335.155034.151493.81 485.37 3220.38
0.00-2465.845055.721524.22 0.0 3125.11
-770.25-2370.585110.541489.22 -483.88 3210.49
-1450.67-1996.685060.211498.4 -1088.65 3797.4
-1648.41-1197.644177.591430.06 -1968.31 4988.31
-1834.43-596.043954.69758.73 -2335.15 5034.15
-1852.990.003799.200.0 -2465.84 5055.72
-1807.500.003130.68-770.25 -2370.58 5110.54
-1738.67-465.873117.69-1450.67 -1996.68 5060.21
-1595.65-921.253191.30-1648.41 -1197.64 4177.59
-1681.15-1681.154117.95-1834.43 -596.04 3954.69
-1359.00-2353.864707.71-1852.99 0.0 3799.2
-758.73-2831.615077.51-1807.5 0.0 3130.68
0.00-2943.505098.29-1738.67 -465.87 3117.69
764.68-2853.835117.34-1595.65 -921.25 3191.3
1463.50-2534.865069.71-1681.15 -1681.15 4117.95
1571.19-1571.193848.62-1359.0 -2353.86 4707.71
1512.95-873.503025.89-758.73 -2831.61 5077.51
1574.46-421.882823.240.0 -2943.5 5098.29
1582.500.002740.97764.68 -2853.83 5117.34
1524.71408.552734.041463.5 -2534.86 5069.71
1484.37857.002968.741571.19 -1571.19 3848.62
956.36956.362342.601512.95 -873.5 3025.89
443.75768.601537.201574.46 -421.88 2823.24
203.17758.251359.661582.5 0.0 2740.97
0.00740.001281.721524.71 408.55 2734.04
-193.21721.061292.981484.37 857.0 2968.74
-409.75709.711419.42956.36 956.36 2342.6
-663.27663.271624.66443.75 768.6 1537.2
-942.24544.001884.47203.17 758.25 1359.66
-1645.94441.032951.410.0 740.0 1281.72
-1713.57762.932888.38-193.21 721.06 1292.98
-1083.05786.882061.45-409.75 709.71 1419.42
-654.52726.921506.25-663.27 663.27 1624.66
-427.54740.521316.71-942.24 544.0 1884.47
-240.67740.711199.30-1645.94 441.03 2951.41
-79.19753.441166.59-1713.57 762.93 2888.38
81.41774.571199.30-1083.05 786.88 2061.45
247.91762.991235.36-654.52 726.92 1506.25
420.19727.791294.07-427.54 740.52 1316.71
650.52722.471497.03-240.67 740.71 1199.3
1119.18813.132130.22-79.19 753.44 1166.59
1474.75656.602485.8281.41 774.57 1199.3
1553.46330.202445.56247.91 762.99 1235.36
1564.750.002409.50420.19 727.79 1294.07
1578.50-335.522484.98650.52 722.47 1497.03
1591.17-708.442682.071119.18 813.13 2130.22
1588.44-1154.073023.411474.75 656.6 2485.82
1392.50-1546.533204.561553.46 330.2 2445.56
1529.89-2649.854711.651564.75 0.0 2409.5
1008.64-3104.275026.151578.5 -335.52 2484.98
348.19-3312.765129.311591.17 -708.44 2682.07
-354.05-3368.565215.691588.44 -1154.07 3023.41
-1031.86-3175.755141.891392.5 -1546.53 3204.56
-1461.27-2530.994500.311529.89 -2649.85 4711.65
-1796.66-1995.404134.651008.64 -3104.27 5026.15
-1678.33-1219.383194.50348.19 -3312.76 5129.31
-1728.50-769.582913.54-354.05 -3368.56 5215.69
-1861.92-395.762931.15-1031.86 -3175.75 5141.89
-1909.500.002940.38-1461.27 -2530.99 4500.31
-1703.140.002260.14-1796.66 -1995.4 4134.65
-1490.57-262.832008.57-1678.33 -1219.38 3194.5
-1684.12-612.972378.34-1728.5 -769.58 2913.54
-1699.07-980.962603.55-1861.92 -395.76 2931.15
-1555.01-1304.812693.80-1909.5 0.0 2940.38
-1695.90-2021.103501.22-1703.14 0.0 2260.14
-1480.16-2563.723928.49-1490.57 -262.83 2008.57
-1272.46-3496.054937.16-1684.12 -612.97 2378.34
-669.45-3796.665116.06-1699.07 -980.96 2603.55
0.00-3939.485227.87-1555.01 -1304.81 2693.8
680.43-3858.895199.92-1695.9 -2021.1 3501.22
1322.89-3634.605132.83-1480.16 -2563.72 3928.49
1700.73-2945.754513.89-1272.46 -3496.05 4937.16
1627.05-1939.043359.06-669.45 -3796.66 5116.06
1623.24-1362.062812.000.0 -3939.48 5227.87
1620.89-935.822483.76680.43 -3858.89 5199.92
1626.44-591.982296.881322.89 -3634.6 5132.83
1615.62-284.882177.081700.73 -2945.75 4513.89
1629.110.002161.911627.05 -1939.04 3359.06
1568.21276.522113.191623.24 -1362.06 2812.0
1468.66534.552074.061620.89 -935.82 2483.76
1464.54845.552244.171626.44 -591.98 2296.88
1020.69856.461768.181615.62 -284.88 2177.08
669.23797.561381.641629.11 0.0 2161.91
459.18795.331218.721568.21 276.52 2113.19
292.69804.171135.661468.66 534.55 2074.06
126.76718.91968.741464.54 845.55 2244.17
0.00785.371042.221020.69 856.46 1768.18
-135.96771.071039.02669.23 797.56 1381.64
-272.73749.321058.19459.18 795.33 1218.72
-404.72701.001074.16292.69 804.17 1135.66
-615.07733.021269.83126.76 718.91 968.74
-848.27711.781469.490.0 785.37 1042.22
-1692.29977.052593.17-135.96 771.07 1039.02
-1707.87621.622411.88-272.73 749.32 1058.19
-404.72 701.0 1074.16
-615.07 733.02 1269.83
-848.27 711.78 1469.49
-1692.29 977.05 2593.17
-1707.87 621.62 2411.88

原文地址:https://blog.csdn.net/caoyanchao1/article/details/137828403

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!