Excel文件转Asc文件

发布于:2024-04-17 ⋅ 阅读:(111) ⋅ 点赞:(0)

单个转换

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文件数据

x y z NCOLS 3
0.00 0.00 4836.00 NROWS 169
-427.85 0.00 4890.32 XLLCENTER 0
-133.86 -411.96 4951.09 YLLCENTER 0
348.46 -253.17 4923.19 CELLSIZE 1
343.03 249.23 4846.49 NODATA_VALUE -9999
-287.29 884.19 10626.41 0.0 0.0 4836.0
-1503.88 1092.64 10542.37 -427.85 0.0 4890.32
-259.72 799.32 4766.47 -133.86 -411.96 4951.09
257.84 793.54 4732.00 348.46 -253.17 4923.19
1523.83 1107.13 10682.21 343.03 249.23 4846.49
864.25 0.00 4901.39 -287.29 884.19 10626.41
700.88 -509.22 4913.21 -1503.88 1092.64 10542.37
266.53 -820.30 4891.54 -259.72 799.32 4766.47
-269.32 -828.88 4942.75 257.84 793.54 4732.0
-703.41 -511.05 4930.93 1523.83 1107.13 10682.21
-869.46 0.00 4930.93 864.25 0.0 4901.39
-1223.64 0.00 4907.76 700.88 -509.22 4913.21
-1078.98 -622.95 4997.02 266.53 -820.3 4891.54
-619.08 -1072.27 4965.97 -269.32 -828.88 4942.75
0.00 -1244.93 4993.14 -703.41 -511.05 4930.93
618.59 -1071.44 4962.09 -869.46 0.0 4930.93
1070.81 -618.23 4959.18 -1223.64 0.0 4907.76
1206.22 0.00 4837.89 -1078.98 -622.95 4997.02
1050.49 606.50 4865.06 -619.08 -1072.27 4965.97
513.96 890.21 4122.79 0.0 -1244.93 4993.14
0.00 848.90 3404.77 618.59 -1071.44 4962.09
-432.19 748.58 3466.87 1070.81 -618.23 4959.18
-1051.53 607.10 4869.91 1206.22 0.0 4837.89
-1443.41 642.65 4862.75 1050.49 606.5 4865.06
-684.00 759.66 3146.09 513.96 890.21 4122.79
-222.30 684.18 2214.06 0.0 848.9 3404.77
79.69 758.17 2346.26 -432.19 748.58 3466.87
435.87 754.95 2682.93 -1051.53 607.1 4869.91
1264.00 918.35 4808.54 -1443.41 642.65 4862.75
1408.25 299.33 4430.97 -684.0 759.66 3146.09
1424.87 -302.87 4483.28 -222.3 684.18 2214.06
1272.25 -924.34 4839.93 79.69 758.17 2346.26
816.11 -1413.55 5023.48 435.87 754.95 2682.93
170.45 -1621.75 5018.73 1264.0 918.35 4808.54
-499.23 -1536.47 4972.12 1408.25 299.33 4430.97
-1088.45 -1208.85 5006.36 1424.87 -302.87 4483.28
-1447.64 -644.53 4877.02 1272.25 -924.34 4839.93
-1511.09 0.00 4650.67 816.11 -1413.55 5023.48
-1682.36 0.00 4163.98 170.45 -1621.75 5018.73
-1561.19 -568.23 4112.06 -499.23 -1536.47 4972.12
-1389.77 -1166.16 4490.35 -1088.45 -1208.85 5006.36
-1022.11 -1770.35 5059.64 -1447.64 -644.53 4877.02
-356.02 -2019.07 5074.48 -1511.09 0.0 4650.67
353.61 -2005.42 5040.17 -1682.36 0.0 4163.98
967.42 -1675.62 4788.90 -1561.19 -568.23 4112.06
1443.15 -1210.95 4662.81 -1389.77 -1166.16 4490.35
1538.31 -559.90 4051.79 -1022.11 -1770.35 5059.64
1480.82 0.00 3665.16 -356.02 -2019.07 5074.48
1602.72 583.34 4221.47 353.61 -2005.42 5040.17
1087.89 912.84 3514.95 967.42 -1675.62 4788.9
511.15 885.34 2530.28 1443.15 -1210.95 4662.81
141.55 802.76 2017.55 1538.31 -559.9 4051.79
-136.08 771.77 1939.67 1480.82 0.0 3665.16
-409.82 709.83 2028.68 1602.72 583.34 4221.47
-778.82 653.51 2516.38 1087.89 912.84 3514.95
-1771.69 644.84 4666.52 511.15 885.34 2530.28
-1691.84 549.71 3647.31 141.55 802.76 2017.55
-1054.02 765.79 2671.22 -136.08 771.77 1939.67
-556.05 765.33 1939.60 -409.82 709.83 2028.68
-258.06 794.22 1712.20 -778.82 653.51 2516.38
0.00 768.46 1575.59 -1771.69 644.84 4666.52
252.10 775.88 1672.66 -1691.84 549.71 3647.31
570.22 784.84 1989.03 -1054.02 765.79 2671.22
1316.46 956.46 3336.32 -556.05 765.33 1939.6
1493.81 485.37 3220.38 -258.06 794.22 1712.2
1524.22 0.00 3125.11 0.0 768.46 1575.59
1489.22 -483.88 3210.49 252.1 775.88 1672.66
1498.40 -1088.65 3797.40 570.22 784.84 1989.03
1430.06 -1968.31 4988.31 1316.46 956.46 3336.32
758.73 -2335.15 5034.15 1493.81 485.37 3220.38
0.00 -2465.84 5055.72 1524.22 0.0 3125.11
-770.25 -2370.58 5110.54 1489.22 -483.88 3210.49
-1450.67 -1996.68 5060.21 1498.4 -1088.65 3797.4
-1648.41 -1197.64 4177.59 1430.06 -1968.31 4988.31
-1834.43 -596.04 3954.69 758.73 -2335.15 5034.15
-1852.99 0.00 3799.20 0.0 -2465.84 5055.72
-1807.50 0.00 3130.68 -770.25 -2370.58 5110.54
-1738.67 -465.87 3117.69 -1450.67 -1996.68 5060.21
-1595.65 -921.25 3191.30 -1648.41 -1197.64 4177.59
-1681.15 -1681.15 4117.95 -1834.43 -596.04 3954.69
-1359.00 -2353.86 4707.71 -1852.99 0.0 3799.2
-758.73 -2831.61 5077.51 -1807.5 0.0 3130.68
0.00 -2943.50 5098.29 -1738.67 -465.87 3117.69
764.68 -2853.83 5117.34 -1595.65 -921.25 3191.3
1463.50 -2534.86 5069.71 -1681.15 -1681.15 4117.95
1571.19 -1571.19 3848.62 -1359.0 -2353.86 4707.71
1512.95 -873.50 3025.89 -758.73 -2831.61 5077.51
1574.46 -421.88 2823.24 0.0 -2943.5 5098.29
1582.50 0.00 2740.97 764.68 -2853.83 5117.34
1524.71 408.55 2734.04 1463.5 -2534.86 5069.71
1484.37 857.00 2968.74 1571.19 -1571.19 3848.62
956.36 956.36 2342.60 1512.95 -873.5 3025.89
443.75 768.60 1537.20 1574.46 -421.88 2823.24
203.17 758.25 1359.66 1582.5 0.0 2740.97
0.00 740.00 1281.72 1524.71 408.55 2734.04
-193.21 721.06 1292.98 1484.37 857.0 2968.74
-409.75 709.71 1419.42 956.36 956.36 2342.6
-663.27 663.27 1624.66 443.75 768.6 1537.2
-942.24 544.00 1884.47 203.17 758.25 1359.66
-1645.94 441.03 2951.41 0.0 740.0 1281.72
-1713.57 762.93 2888.38 -193.21 721.06 1292.98
-1083.05 786.88 2061.45 -409.75 709.71 1419.42
-654.52 726.92 1506.25 -663.27 663.27 1624.66
-427.54 740.52 1316.71 -942.24 544.0 1884.47
-240.67 740.71 1199.30 -1645.94 441.03 2951.41
-79.19 753.44 1166.59 -1713.57 762.93 2888.38
81.41 774.57 1199.30 -1083.05 786.88 2061.45
247.91 762.99 1235.36 -654.52 726.92 1506.25
420.19 727.79 1294.07 -427.54 740.52 1316.71
650.52 722.47 1497.03 -240.67 740.71 1199.3
1119.18 813.13 2130.22 -79.19 753.44 1166.59
1474.75 656.60 2485.82 81.41 774.57 1199.3
1553.46 330.20 2445.56 247.91 762.99 1235.36
1564.75 0.00 2409.50 420.19 727.79 1294.07
1578.50 -335.52 2484.98 650.52 722.47 1497.03
1591.17 -708.44 2682.07 1119.18 813.13 2130.22
1588.44 -1154.07 3023.41 1474.75 656.6 2485.82
1392.50 -1546.53 3204.56 1553.46 330.2 2445.56
1529.89 -2649.85 4711.65 1564.75 0.0 2409.5
1008.64 -3104.27 5026.15 1578.5 -335.52 2484.98
348.19 -3312.76 5129.31 1591.17 -708.44 2682.07
-354.05 -3368.56 5215.69 1588.44 -1154.07 3023.41
-1031.86 -3175.75 5141.89 1392.5 -1546.53 3204.56
-1461.27 -2530.99 4500.31 1529.89 -2649.85 4711.65
-1796.66 -1995.40 4134.65 1008.64 -3104.27 5026.15
-1678.33 -1219.38 3194.50 348.19 -3312.76 5129.31
-1728.50 -769.58 2913.54 -354.05 -3368.56 5215.69
-1861.92 -395.76 2931.15 -1031.86 -3175.75 5141.89
-1909.50 0.00 2940.38 -1461.27 -2530.99 4500.31
-1703.14 0.00 2260.14 -1796.66 -1995.4 4134.65
-1490.57 -262.83 2008.57 -1678.33 -1219.38 3194.5
-1684.12 -612.97 2378.34 -1728.5 -769.58 2913.54
-1699.07 -980.96 2603.55 -1861.92 -395.76 2931.15
-1555.01 -1304.81 2693.80 -1909.5 0.0 2940.38
-1695.90 -2021.10 3501.22 -1703.14 0.0 2260.14
-1480.16 -2563.72 3928.49 -1490.57 -262.83 2008.57
-1272.46 -3496.05 4937.16 -1684.12 -612.97 2378.34
-669.45 -3796.66 5116.06 -1699.07 -980.96 2603.55
0.00 -3939.48 5227.87 -1555.01 -1304.81 2693.8
680.43 -3858.89 5199.92 -1695.9 -2021.1 3501.22
1322.89 -3634.60 5132.83 -1480.16 -2563.72 3928.49
1700.73 -2945.75 4513.89 -1272.46 -3496.05 4937.16
1627.05 -1939.04 3359.06 -669.45 -3796.66 5116.06
1623.24 -1362.06 2812.00 0.0 -3939.48 5227.87
1620.89 -935.82 2483.76 680.43 -3858.89 5199.92
1626.44 -591.98 2296.88 1322.89 -3634.6 5132.83
1615.62 -284.88 2177.08 1700.73 -2945.75 4513.89
1629.11 0.00 2161.91 1627.05 -1939.04 3359.06
1568.21 276.52 2113.19 1623.24 -1362.06 2812.0
1468.66 534.55 2074.06 1620.89 -935.82 2483.76
1464.54 845.55 2244.17 1626.44 -591.98 2296.88
1020.69 856.46 1768.18 1615.62 -284.88 2177.08
669.23 797.56 1381.64 1629.11 0.0 2161.91
459.18 795.33 1218.72 1568.21 276.52 2113.19
292.69 804.17 1135.66 1468.66 534.55 2074.06
126.76 718.91 968.74 1464.54 845.55 2244.17
0.00 785.37 1042.22 1020.69 856.46 1768.18
-135.96 771.07 1039.02 669.23 797.56 1381.64
-272.73 749.32 1058.19 459.18 795.33 1218.72
-404.72 701.00 1074.16 292.69 804.17 1135.66
-615.07 733.02 1269.83 126.76 718.91 968.74
-848.27 711.78 1469.49 0.0 785.37 1042.22
-1692.29 977.05 2593.17 -135.96 771.07 1039.02
-1707.87 621.62 2411.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

网站公告

今日签到

点亮在社区的每一天
去签到