简体   繁体   English

如何在python中的txt文件中的特定位置读取数字?

[英]How to read the numeric number in a specific location in a txt file in python?

I have an output txt file and I only want the number 13.0 in line 151 我有一个输出txt文件,我只希望行151中的数字13.0

13.0 = ebeam1 ! 13.0 = ebeam1! beam 1 total energy in GeV 束1的总能量(GeV)

and 74.761227 line 479 和74.761227行479

# Integrated weight (pb) : 74.761227 #综合重量(pb):74.761227

I wonder how to read these numbers and write them as a line in another file? 我想知道如何读取这些数字并将它们写为另一文件中的一行吗?

You want to use the linecache module. 您要使用linecache模块。

import linecache
line = linecache.get('path/to/file', 479)

Then write it to another file. 然后将其写入另一个文件。

with open('other/file.txt', 'w') as f:
    f.write(line)

Assuming you want to extract just the number portion of the line: 假设您只想提取行的数字部分:

import re
In [4]: re.search(r'(\d+.*\d*$)', line).group()
Out[4]: '74.761227'

Cory Madden's answer will work, but if you didn't know what number the line you're looking for is on you could do something like: 科里·马登(Cory Madden)的答案会起作用,但是如果您不知道要寻找的电话号码,则可以执行以下操作:

import re

regex = re.compile(r"# Integrated weight \(pb\) : (?P<number>-?\d+\.?\d*)")

with open(file_path, "r") as lines:
    for line in lines:
        match = re.match(regex, line)
        if match:
            number = float(match.group("number"))
            return number
import linecache
import re

ln = linecache.getline('so_qn_test_file.txt', 479)
nums = re.findall(r"[-+]?\d*\.\d+|[-+]?\d+", ln)
# print(nums)
with open('op_file.txt','w') as f:
    f.write(','.join(nums))

This will work. 这将起作用。 I have already tested it. 我已经测试过了。 For explanation for Regex , this should help. 对于Regex的解释, 应该会有所帮助。

Thank God for your question, you will basically use a for loop to loop through the lines in the file and add each line to a list. 谢谢上帝的提问,您基本上将使用for循环遍历文件中的各行,并将每一行添加到列表中。 So that at anytime you can call the list with the line number you want and that particular line will be delivered to you. 因此,您可以随时用所需的行号呼叫列表,该特定行将被发送给您。 You then save it in a variable and then apply regular expression(regex) on it, to get only the floating numbers. 然后,将其保存在变量中,然后在其上应用正则表达式(regex),以仅获取浮点数。

example txt file: txt文件示例:

151 Jesus 13.0
152 John
153 Peter 74.745392

then in your python file 然后在你的python文件中

import re

file_line_arr = []
with open('example.txt', 'r') as file:
    for line in file:
        file_line_arr.append(line)
line_1 = file_line_arr[151-1]
line_3 = file_line_arr[153-1]

first_number = re.findall('\d+.?\d+', line_1 )
second_number = re.findall('\d+.?\d+', line_3)

first_number_wq = re.sub("'", "", str(first_number))
first_number_wb = re.sub('(\[|\])', '', first_number_wq)

second_number_wq = re.sub("'", "", str(second_number))
second_number_wb = re.sub('(\[|\])', '', second_number_wq)

with open('new_file.txt', 'w') as new_file:
    new_file.write(first_number_wb + '\n' + second_number_wb)

Here is a book on python that you will like very much - ( dive into python3 ) search for it 这是一本关于python的书,您会非常喜欢-( 深入python3 )进行搜索

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM