如何使用python提取文件中的某些文本? [英] how to use python to extract certain text in the file?

查看:118
本文介绍了如何使用python提取文件中的某些文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提取文本文件的某些部分。我的输入文件:


- num cell port function safe [ccell disval rslt]

" 17(BC_1,CLK,input,X)," ; &

" 16(BC_1,OC_NEG,输入,X)," & - 合并输入/

" 8(BC_1,D(8),输入,X), &安培; - 单元格16 @ 1 - > Hi-Z

" 7(BC_1,Q(1),output3,X,16,1,Z), &

" 0(BC_1,Q(8),output3,X,16,1,Z)" ;;

我需要输出如下:


num cell port function safe ccell

17 BC_1 CLK输入X

16 BC_1 OC_NEG输入X

16 BC_1 *控制1
8 BC_1 D8输入X

7 BC_1 Q1输出3 X 16 1

0 BC_1 Q8输出3 X 16 1

目前为止我试过下面的代码,但它给出了索引错误。请指教。

i want to extract certain section of the text file. my input file:

-- num cell port function safe [ccell disval rslt]
"17 (BC_1, CLK, input, X)," &
"16 (BC_1, OC_NEG, input, X), " &-- Merged input/
" 8 (BC_1, D(8), input, X)," & -- cell 16 @ 1 -> Hi-Z
" 7 (BC_1, Q(1), output3, X, 16, 1, Z)," &
" 0 (BC_1, Q(8), output3, X, 16, 1, Z)";
and i need the output to be as such:

num cell port function safe ccell
17 BC_1 CLK input X
16 BC_1 OC_NEG input X
16 BC_1 * control 1
8 BC_1 D8 input X
7 BC_1 Q1 output3 X 16 1
0 BC_1 Q8 output3 X 16 1
so far i tried below code but it gave index error. pls advise.

展开 | 选择 < span class =codeDivider> | Wrap | 行号

推荐答案

我认为这是因为只有5个元素。 17 BC_1 CLK输入X并且您正在尝试打印7(a [0]到[6])。
I believe it is because there are only 5 elements in "17 BC_1 CLK input X" and you are trying to print 7 (a[0] to a[6]).


谢谢将重新查看它。


简单的方法更安全地执行此操作将类似于
The easy way to do this more safely would be something like
展开 | 选择 | Wrap | 行号


这篇关于如何使用python提取文件中的某些文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆