Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 需要帮助确定文本文件中文本的常规格式吗_Python_String_Format - Fatal编程技术网

Python 需要帮助确定文本文件中文本的常规格式吗

Python 需要帮助确定文本文件中文本的常规格式吗,python,string,format,Python,String,Format,如果有人有经验的话,这个问题与HECRAS有关,但一般来说,这只是一个关于将文本文件写入HECRAS软件读取的非常特殊的格式的问题 基本上我在读一些文件,修改一些数字,然后写出来,但我似乎无法完全匹配初始格式 以下是原始文件的外观: Type RM Length L Ch R = 1 ,229.41 ,21276,21276,21276 Node Last Edited Time=Oct-17-2019 15:52:28 #Sta/Elev= 452 0 20.097 67

如果有人有经验的话,这个问题与HECRAS有关,但一般来说,这只是一个关于将文本文件写入HECRAS软件读取的非常特殊的格式的问题

基本上我在读一些文件,修改一些数字,然后写出来,但我似乎无法完全匹配初始格式

以下是原始文件的外观:

Type RM Length L Ch R = 1 ,229.41  ,21276,21276,21276
Node Last Edited Time=Oct-17-2019 15:52:28
#Sta/Elev= 452 
       0  20.097  67.042   9.137   67.43   9.139  68.208   9.073  68.598   9.129
  68.986   9.086  70.538   9.071  70.926   9.042  71.984   9.046   72.48   9.025
  73.646   9.056  74.368   9.034  75.586   9.042   76.55   9.017  77.138   9.047
  78.304   8.989   79.47   9.025   80.19   9.001   81.41   9.003  81.974   8.978
   83.83   9.005  85.284   9.079  85.682   9.068   86.97   9.118  88.012   9.223
   88.79   9.239   89.65   9.316  90.342   9.324  91.134   9.475  91.966   9.525
  92.282   9.589  93.346   9.546  94.222   9.557  94.922   9.594   95.71   9.591
  96.546    9.64  97.286   9.574   98.87   9.688  99.258   9.673  99.642   9.712
#Mann= 3 , 0 , 0 
       0     .09       0   246.4    .028       0   286.4     .09       0
Bank Sta=246.4,286.4
XS Rating Curve= 0 ,0
XS HTab Starting El and Incr=1.708,0.1, 500 
XS HTab Horizontal Distribution= 5 , 5 , 5 
Exp/Cntr=0.3,0.1
我对Sta/Elev数据感兴趣…它看起来像是一些右对齐的选项卡/空间?每行5个站点/高程对的分隔格式。可能每对16个字符

我尝试了很多不同的方法,我目前的代码是:

with open('C:/Users/deden/Desktop/t/test.g01','w') as out:
    out.write(txt[:idx[0][0]])
    out.write(txt[idx[0][0]:idx[0][0]+bounds[0]])
    out.write('#'+raw_SE.split('\n')[0]+'\n')
    i = 0 
    while i <= len(new_SE):
        out.write('\t'.join(new_SE[i:i+10])+'\n')
        i+=10
    out.write(txt[idx[0][0]+bounds[1]:idx[1][0]])
我还试着用类似的东西来玩弄正义的一面:

'%8s  %8s' % (tmp[0], tmp[1])
文本之间基本上有8个空格,但右对齐

老实说,如果有人能在#Sta/Elev=452和#Mann之间重新创建原始文本,我将万分感激,如果有人想尝试一下,下面是完整的列表:

new_SE = ['0', '30.097', '67.042', '19.137', '67.43', '19.139', '68.208', '19.073', '68.598', '19.128999999999998', '68.986', '19.086', '70.538', '19.070999999999998', '70.926', '19.042', '71.984', '19.046', '72.48', '19.025', '73.646', '19.055999999999997', '74.368', '19.034', '75.586', '19.042', '76.55', '19.017', '77.138', '19.047', '78.304', '18.989', '79.47', '19.025', '80.19', '19.000999999999998', '81.41', '19.003', '81.974', '18.978', '83.83', '19.005000000000003', '85.284', '19.079', '85.682', '19.067999999999998', '86.97', '19.118000000000002', '88.012', '19.223', '88.79', '19.239', '89.65', '19.316000000000003', '90.342', '19.323999999999998', '91.134', '19.475', '91.966', '19.525', '92.282', '19.589', '93.346', '19.546', '94.222', '19.557000000000002', '94.922', '19.594', '95.71', '19.591', '96.546', '19.64', '97.286', '19.573999999999998', '98.87', '19.688000000000002', '99.258', '19.673000000000002', '99.642', '19.712']

不确定我是否理解正确-请考虑看< /P>
# with open('C:/Users/deden/Desktop/t/test.g01','w') as out:
    for i in range(0, len(new_SE), 10):
        row = [f'{float(v):8.3f}' for v in new_SE[i:i+10]]
        out.write(''.join(r) + '\n')

#    0.000  30.097  67.042  19.137  67.430  19.139  68.208  19.073  68.598  19.129
#   68.986  19.086  70.538  19.071  70.926  19.042  71.984  19.046  72.480  19.025
#   73.646  19.056  74.368  19.034  75.586  19.042  76.550  19.017  77.138  19.047
#   78.304  18.989  79.470  19.025  80.190  19.001  81.410  19.003  81.974  18.978
#   83.830  19.005  85.284  19.079  85.682  19.068  86.970  19.118  88.012  19.223
#   88.790  19.239  89.650  19.316  90.342  19.324  91.134  19.475  91.966  19.525
#   92.282  19.589  93.346  19.546  94.222  19.557  94.922  19.594  95.710  19.591
#   96.546  19.640  97.286  19.574  98.870  19.688  99.258  19.673  99.642  19.712
new_SE = ['0', '30.097', '67.042', '19.137', '67.43', '19.139', '68.208', '19.073', '68.598', '19.128999999999998', '68.986', '19.086', '70.538', '19.070999999999998', '70.926', '19.042', '71.984', '19.046', '72.48', '19.025', '73.646', '19.055999999999997', '74.368', '19.034', '75.586', '19.042', '76.55', '19.017', '77.138', '19.047', '78.304', '18.989', '79.47', '19.025', '80.19', '19.000999999999998', '81.41', '19.003', '81.974', '18.978', '83.83', '19.005000000000003', '85.284', '19.079', '85.682', '19.067999999999998', '86.97', '19.118000000000002', '88.012', '19.223', '88.79', '19.239', '89.65', '19.316000000000003', '90.342', '19.323999999999998', '91.134', '19.475', '91.966', '19.525', '92.282', '19.589', '93.346', '19.546', '94.222', '19.557000000000002', '94.922', '19.594', '95.71', '19.591', '96.546', '19.64', '97.286', '19.573999999999998', '98.87', '19.688000000000002', '99.258', '19.673000000000002', '99.642', '19.712']
# with open('C:/Users/deden/Desktop/t/test.g01','w') as out:
    for i in range(0, len(new_SE), 10):
        row = [f'{float(v):8.3f}' for v in new_SE[i:i+10]]
        out.write(''.join(r) + '\n')

#    0.000  30.097  67.042  19.137  67.430  19.139  68.208  19.073  68.598  19.129
#   68.986  19.086  70.538  19.071  70.926  19.042  71.984  19.046  72.480  19.025
#   73.646  19.056  74.368  19.034  75.586  19.042  76.550  19.017  77.138  19.047
#   78.304  18.989  79.470  19.025  80.190  19.001  81.410  19.003  81.974  18.978
#   83.830  19.005  85.284  19.079  85.682  19.068  86.970  19.118  88.012  19.223
#   88.790  19.239  89.650  19.316  90.342  19.324  91.134  19.475  91.966  19.525
#   92.282  19.589  93.346  19.546  94.222  19.557  94.922  19.594  95.710  19.591
#   96.546  19.640  97.286  19.574  98.870  19.688  99.258  19.673  99.642  19.712