- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我试图过滤一些 .txt 文件,这些文件以 YYYYMMDD 格式的日期命名,并且包含一些关于太阳事件区域的数据。我编写了一个代码,给定一个 YYYYMMDD 格式的日期,它可以列出一个时间范围内的文件,我希望我正在寻找的事件区域是这样的,并根据该条目解析信息。这些 txt 的示例可以在下面看到,有关它的更多信息(如果您感到好奇)可以在 SWPC website 上看到。 .
:Product: 0509SRS.txt
:Issued: 2012 May 09 0030 UTC
# Prepared jointly by the U.S. Dept. of Commerce, NOAA,
# Space Weather Prediction Center and the U.S. Air Force.
#
Joint USAF/NOAA Solar Region Summary
SRS Number 130 Issued at 0030Z on 09 May 2012
Report compiled from data received at SWO on 08 May
I. Regions with Sunspots. Locations Valid at 08/2400Z
Nmbr Location Lo Area Z LL NN Mag Type
1470 S19W68 284 0030 Cro 02 02 Beta
1471 S22W60 277 0120 Cso 05 03 Beta
1474 N14W13 229 0010 Axx 00 01 Alpha
1476 N11E35 181 0940 Fkc 17 33 Beta-Gamma-Delta
1477 S22E73 144 0060 Hsx 03 01 Alpha
IA. H-alpha Plages without Spots. Locations Valid at 08/2400Z May
Nmbr Location Lo
1472 S28W80 297
1475 N05W05 222
II. Regions Due to Return 09 May to 11 May
Nmbr Lat Lo
1460 N16 126
1459 S16 110
我用来解析这些 txt 文件的代码是:
import glob
def seeker(noaa_number, t_start, path = None):
'''
This function will open an SRS file
and look for each line if the given AR
(specified by its NOAA number) is there.
If so, this function should grab the
entries and return them.
'''
#defaulting path if none is given
if path is None:
#assigning
path = 'defaultpath'
#listing the items within the directory
files = sorted(glob.glob(path+'*.txt'))
#finding the index in the list of
#the starting time
index = files.index(path+str(t_start)+'SRS.txt')
#looping over each file
for file in files[index: index+20]:
#opening file
f = open(file, 'r')
#reading the lines
text = f.readlines()
#looping over each line in the text
for line in text:
#checking if the noaa number is mentioned
#in the given line
if noaa_number in line:
#test print
print('Original line: ', line)
#slicing the text to get the column values
nbr = line[:4]
Location = line[5:11]
Lo = line[14:18]
Area = line[19:23]
Z = line[24:28]
LL = line[29:31]
NN = line[34:36]
MagType = line[37:]
#test prints
print('nbr: ', nbr)
print('location: ', Location)
print('Lo: ', Lo)
print('Area: ', Area)
print('Z: ', Z)
print('LL: ', LL)
print('NN: ', NN)
print('MagType: ', MagType)
return
我对此进行了测试,它可以正常工作,但我有点笨,原因有二:
尽管这些文件是按照标准制作的,但考虑到我按索引对数组进行切片的方式,只需要一个额外的空间就会使代码崩溃。有更好的选择吗?
表 IA 和 II 上的信息与我无关,因此理想情况下,我想阻止我的代码扫描它们。由于第一列的行数不同,是否可以告诉代码何时停止阅读给定文档?
感谢您的宝贵时间!
最佳答案
您可以使用 .split()
方法将行拆分为列表,而不是按绝对位置进行切片。这将对额外的空间具有鲁棒性。
所以代替
Location = line[5:11]
Lo = line[14:18]
Area = line[19:23]
Z = line[24:28]
LL = line[29:31]
NN = line[34:36]
你可以使用
Location = line.split()[1]
Lo = line.split()[2]
Area = line.split()[3]
Z = line.split()[4]
LL = line.split()[5]
NN = line.split()[6]
如果你想让它更快,你可以拆分列表一次,然后只从同一个列表中提取相关数据,而不是每次都拆分它:
data = line.split()
Location = data[1]
Lo = data[2]
Area = data[3]
Z = data[4]
LL = data[5]
NN = data[6]
要阻止它在传递相关数据后继续读取文件,您可以在它不再在行中找到 noaa_number 时退出循环
# In the file function but before looping through the lines.
started_reading = False ## Set this to false so
## that it doesn't exit
## before it gets to the
## relevant data
for line in text:
if noaa_number in line:
started_reading = True
## Parsing stuff
elif started_reading is True:
break # exits the loop
关于python - 过滤和解析 Solar Region Summary 文件中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52203111/
这是我的进口: import tensorflow as tf import keras from keras.models import Sequential, Model from keras.l
我收到此错误,尽管我在所有地方都提到 file_writer = tf.summary.FileWriter('/path/to/logs', sess.graph) 是正确的实现的this和 thi
我一直在尝试研究 tensorboard(来自 tensorflow(从 github fork ,也使用最新版本),每当我使用“标量”或“直方图”函数时都会出现上述错误。为什么标量函数指向 te
我尝试通过创建一个 css 并将其应用于验证摘要来解决此问题。以下解决方案仅适用于 Firefox 和 Google Chrome,但不适用于 IE(7、8 和 9)。以前有人遇到过这种情况吗? 这是
有人可以解释为什么 R 这样做吗?在整数值上舍入最大值和最小值似乎非常有缺陷。 summary(1:1283932) Min. 1st Qu. Median Mean 3rd Qu.
我有一个棘手的问题,我想生成几段给定 url 的“描述”,通常是文章的开头。元描述字段是一种方法,但它并不总是好的或设置正确。 公平地说,从屏幕抓取的 HTML 中完成此操作有点问题。我有一个大致的想
我正在使用 AFNetworking 从我们的服务器获取 JSON 提要,然后我将 AFNetworking 给我的响应对象转换为 NSArray,我之前已经做过一百万次,没有问题。 JSON 只是一
summary():获取描述性统计量,可以提供最小值、最大值、四分位数和数值型变量的均值,以及因子向量和逻辑型向量的频数统计等。 结果解读如下: 1. 调用:Call lm(
题目地址:https://leetcode.com/problems/summary-ranges/description/ 题目描述: Given a sorted integer array
我发现(见下文)函数 summary.rq (第 88 页)来自 quantreg 包打印不同的输出,这取决于样本量是大于等于还是小于 1001。 我知道,rq() 使用不同的方法,具体取决于样本大小
我正在使用子类化 API 构建一个简单的转换网络,我想使用摘要方法来了解我的模型的架构。但是,当我调用 model.summary() 时,图层乱序并且输出形状也没有显示。有没有一种干净的方法来解决这
[对于那些不在家的人,这是 Rolling My Own Version Control 的续集;)] 所以我放弃并安装了 TortoiseSVN(为了和 friend 一起做一个项目,我的个人版本控
我正在对包含 1000 多个变量的数据集运行主成分分析。我正在使用 R Studio,当我运行摘要以查看组件的累积方差时,我只能看到最后几百个组件。如何限制摘要仅显示前 100 个组件? 最佳答案 我
我阅读了这两个文档页面:summary和 summary-card-with-large-image ,但我真的看不出有什么区别。 例子: 最后两者之间的实际区别是什么?渲染看起来相同:
我希望我的 lm 摘要输出比平时更紧凑。我想删除一些换行符,“残差”部分,带有“系数”一词的行。积极的一面是,summary.lm 是作为原生 R 函数编写的,所以大概我可以将它复制到一个文件中,更改
我在使用 标签时遇到问题。我的问题出在 Jupyter Notebook 中,但它是可重现的 here . 当我在 Markdown 单元格中有以下内容时: Dropdown label H
我将keras模型定义如下 class ConvLayer(Layer) : def __init__(self, nf, ks=3, s=2, **kwargs): self
好吧,我已经阅读了很多有关此问题的帖子和资源,但我仍然遇到同样的问题。我有一些 NSString 变量,我需要将它们作为类范围的变量在整个类的多个位置使用。我尝试了很多设置组合。首先,我确实在界面中声
我的代码经常使用 tapply 和 summary 如下所示: data % map(~ as.numeric(round(.x, 2))) %>% map_dfr(set_names, na
我经常想记录 python 变量——而不是 tf 张量。 在文档中,它说“您可以传递用自己的数据填充的 tf.Summary Protocol Buffer ”,但没有 tf.Summary 的文档我
我是一名优秀的程序员,十分优秀!