- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我对 python 很陌生,没有真正的编程知识。在我目前的工作中,我被要求从大约 500 多个文件中获取文本形式的数据并将它们绘制出来。我在一定程度上理解绘图,但我似乎无法弄清楚如何以易于选择特定部分的方式操作数据。目前,这是我打开文件的方法:
fp=open("file")
for line in fp:
words = line.strip().split()
print words
结果是它为我提供了文件每一行的列表,但我只能访问最后一行。有人知道一种方法可以让我选择列表的不同变体吗?非常感谢!!
最佳答案
从文件中获取行列表的最简单方法如下:
with open('file', 'r') as f:
lines = f.readlines()
现在您可以拆分这些行或对它们执行任何您想要的操作:
lines = [line.split() for line in lines]
我不确定这是否能回答您的问题 - 如果您有更具体的想法,请告诉我。
<小时/>由于我完全不明白您所问的内容,因此这里有一些有关如何处理文本文件的示例。您可以在交互式解释器中进行实验,通常只需在命令行中键入“python”即可访问该解释器。
>>> with open('a_text_file.txt', 'r') as f:
... text = f.read()
...
>>> text
'the first line of the text file\nthe second line -- broken by a symbol\nthe third line of the text file\nsome other data\n'
这是文件的原始、未处理的文本。这是一个字符串。字符串是不可变的——它们不能被改变——但它们可以部分或全部复制。
>>> text.splitlines()
['the first line of the text file', 'the second line -- broken by a symbol', 'the third line of the text file', 'some other data']
splitlines
是一个字符串方法。 splitlines
在找到 \n
(换行符)字符的地方分割字符串;然后它返回一个列表,其中包含字符串各个部分的副本。
>>> lines = text.splitlines()
这里我刚刚将上面的行列表保存到一个新的变量名称中。
>>> lines[0]
'the first line of the text file'
列表通过索引访问。只需提供从 0
到 len(lines) - 1
的整数即可返回相应的行。
>>> lines[2]
'the third line of the text file'
>>> lines[1]
'the second line -- broken by a symbol'
现在您可以开始操纵单行。
>>> lines[1].split('--')
['the second line ', ' broken by a symbol']
split
是另一种字符串方法。它类似于 splitlines
,但您可以指定要用作分界符的字符或字符串。
>>> lines[1][4]
's'
您还可以对字符串中的字符进行索引。
>>> lines[1][4:10]
'second'
您还可以“切片”字符串。结果是字符 4 到 9 的副本。10 是停止值,因此第 10 个字符不包含在切片中。 (您也可以对列表进行切片。)
>>> lines[1].index('broken')
19
如果要查找字符串中的子字符串,一种方法是使用index
。它返回子字符串第一次出现的索引。 (如果子字符串不在字符串中,它会抛出错误。如果您不想这样做,请使用 find
,如果子字符串不在字符串中,它会返回 -1。)
>>> lines[1][19:]
'broken by a symbol'
然后你可以用它来切片字符串。如果您不提供停止索引,它只会返回字符串的其余部分。
>>> lines[1][:19]
'the second line -- '
如果您不提供开始索引,它将返回字符串的开头并在停止索引处停止。
>>> [line for line in text.splitlines() if 'line' in line]
['the first line of the text file', 'the second line -- broken by a symbol', 'the third line of the text file']
您还可以使用 in
——这是一个 bool 运算,如果子字符串位于字符串中,则返回 True
。在本例中,我使用列表理解来仅获取其中包含 'line'
的行。 (请注意,列表中缺少最后一行。它已被过滤。)
如果您还有其他问题,请告诉我。
关于python - 数据操作: Stemming from a inability to select lists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6586651/
我正在寻找克罗地亚语词干算法的实现。理想情况下使用 Java,但我也接受任何其他语言。 是否有一个讲英语的开发人员社区正在为克罗地亚语开发搜索应用程序? 谢谢 最佳答案 斯拉夫语言高度inflecti
我希望使用基于日期时间的 matplotlib 绘制一个主干。但似乎发生了错误:示例代码: import matplotlib.pyplot as plt from dateutil import p
所以基本上我有两个具有相同数字(16 位数字)的 .txt 文件,前 8 位数字全部相同(例如 12345678),然后接下来的 8 位数字是随机的(例如 38462943)。我一直在尝试做的是将两个
以下问题看起来很像 SO 上已有的许多相关问题(我已经通读了它们,但我的问题略有不同并且仍然存在)。 我使用 stem 库在 Python 3.6.1 中编写了以下代码(在 macOS Sierra
我正在尝试获取 To Russia With Love tutoial来自 Stem 项目工作。 from io import StringIO import socket import urllib
作为代码的一部分,我的功能如下: def match_output(orig_path: Path,lines: Iterable[str],stem: str, delim: str,delim_p
我在将 tor 与 python 结合使用时遇到了一些问题,我不太清楚从这里该何去何从。 系统/包:Windows 10 Python 3.8(通过 jupyter 实验室) 茎 tor(tor.ex
我想将 EELS 数据立方体的强度与厚度图相乘。我尝试使用简单的数学命令,但我只获得了第一个切片的结果。我认为计算就像执行 Result(x,y,E) = SI(x,y,E) * Thickness(
我看过其他关于此问题的帖子,但我仍然无法让它发挥作用。 http://snowball.tartarus.org/dist/libstemmer_java.tgz <<- 这包含 porter2 算法
我正在处理一些 nlp 任务。我的输入是法语文本,因此在我的上下文中只能使用 Snowball Stemmer。但是,不幸的是,它一直给我糟糕的词干,因为它甚至不会删除 plural "s" 或 si
如何在没有标记(仅 Steam 线)的情况下绘制 Steam 图?。它在绘制非常长的信号阵列时特别有用。 谢谢! 最佳答案 您可以简单地将标记设置为空: import matplotlib.pyplo
我正在尝试设置 Makefile 以根据目标文件扩展名构建静态 (.a) 和动态 (.so) 库。 我以前只将以下 Makefile 用于静态库: NAME := config LIB :=
我知道词干提取和词形还原如下: 词干 - 将单词转换成不变的部分;有趣,娱乐 - 有趣 lemmatizing - 将单词转换为字典形式;有趣的,逗乐的 我可以理解为什么要使用词形还原。但我不明白做词
我正在使用 Boost 文件系统库。 我有一条路 boost::filesystem::path P("/foo/bar.baz"); 我想将路径 p 的词干部分修改为“bar_quz”,因此路径 P
我正在尝试使用斯坦福词性标注器和 NER 编写一个关键字提取程序。对于关键字提取,我只对专有名词感兴趣。这是基本方法 通过删除字母以外的任何内容来清理数据 删除停用词 对每个单词进行词干 确定每个单词
我对 python 很陌生,没有真正的编程知识。在我目前的工作中,我被要求从大约 500 多个文件中获取文本形式的数据并将它们绘制出来。我在一定程度上理解绘图,但我似乎无法弄清楚如何以易于选择特定部分
我在远程服务器 (Ubuntu) 上运行 Tor,端口为 9150,控制端口为 9151。我已确认两者都在通过 netstat -ant 运行。 这是我的代码,它引发了 SOCKS5Error: 0x
我正在使用 stem 来控制使用 stem.process.launch_tor_with_config 创建的 tor 节点。我还创建了一个在上述进程的控制端口上运行的 stem.control.C
关闭。这个问题需要更多 focused .它目前不接受答案。 想要改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this q
我一直在尝试将 InceptionResnetV2 中的 Keras implementation 模型摘要与他们论文中指定的模型摘要进行比较,当涉及到 filter_concat block 时,它
我是一名优秀的程序员,十分优秀!