问题标签 [tell]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
4 回答
60175 浏览

python - f.seek() 和 f.tell() 读取文本文件的每一行

我想打开一个文件并使用 and 读取每一f.seek()f.tell()

测试.txt:

我的代码是:

它读取整个文件。

0 投票
3 回答
3482 浏览

perl - perl 读取 seek tell 和 text 文件。读取的字节太多。图层和换行符处理

我有一个 Perl 脚本,它分析一个文本文件(可以是 UNIX 或 Windows 行结尾),当它找到感兴趣的东西时存储文件偏移量。

然后稍后在脚本中,我想生成文本文件的“n”个副本,但在每个“有趣”区域都有附加内容。为了实现这一点,我遍历偏移量的散列:

问题是读取命令正在读取太多字节。我怀疑这是一个行尾问题,因为输出的字节数(字符?)与行号相同。使用记事本++,该tell()命令将实际偏移量返回到兴趣点,但使用该偏移量值read()返回超过兴趣点的字符。

我尝试binmode($fh)在. 这确实在文本文件中找到了正确的位置,但随后我得到 (CR + CRLF) 输出并且文本文件充满了双回车符。open()read()

我玩过层:crlf,:bytes,但没有任何改进。

有点卡住了!

0 投票
2 回答
3880 浏览

python - Python file.tell 给出了错误的值位置

我正在尝试使用 Python 从现有文件中提取多个位置。这是我当前用于提取位置的代码:

这段代码使用不同的搜索值重复了多次(打开的文件更少),并且似乎有效:我得到了正确的消息,并且变量具有值。

但是,使用下面的代码,第一个写入位置是错误的,而随后的写入位置是正确的:

我已经读过,由于 Python 倾向于“提前阅读” ,传递某些read/readline选项可能会导致错误的告诉值。fh我看到的一个避免这种情况的建议是读取整个文件并重写它,这在我的应用程序中不是一个非常吸引人的解决方案。

如果我将第一个代码段更改为:

然后它似乎self.fh.read()只返回字符而不是整行。搜索永远不会匹配。对于self.fh.readline().

我的结论是,fh.tell只有在写操作后查询时才返回有效的文件位置。

有没有办法在阅读/搜索时提取准确的文件位置?

谢谢。

0 投票
2 回答
5010 浏览

applescript - 最佳使用方式:告诉应用程序“系统事件”

我在考虑将此语句用于 GUI 脚本的最佳方法是什么:

有什么区别

此外,似乎“进程”语句可以访问 GUI,而应用程序不能。

0 投票
2 回答
4320 浏览

python - Is there a way to go back when reading a file using seek and calls to next()?

I'm writing a Python script to read a file, and when I arrive at a section of the file, the final way to read those lines in the section depends on information that's given also in that section. So I found here that I could use something like

Yet, the structure of my current code is something like the following:

But this approach gives the following error:

telling position disabled by next() call

Is there a way to prevent this?

0 投票
0 回答
383 浏览

database - Adding clause in Prolog

I am about to write code for my project, which is to create a database in SWI-Prolog. The program should contain ADDING, DELETING, SORTING and SEARCHING entries. My problems are:

  1. I want to ADD 'runners' with capital letters.
  2. When I'm addin/deleting something in database, the file stays either empty or the only entry is the entry I have actually added.

I'm using one file as database entries and the second file for all of the logic. Thanks for helping me in advance.

In the database, there are these records:

The '_' components are for example DATE, HEIGHT, WEIGHT, ... The database file is named database.pl.

In logic, there is this:

Every runner must be written by his name and surname, the other fields can be blank. How to do to add at least Name, Surname and at maximum Name, Surna,Height, Date, Weight... That only first two fields are mandatory others are not.

0 投票
1 回答
594 浏览

applescript - Telling applications to open and then to quit isn't working properly on applescript

This script is to open microsoft apps and then quit after 3 seconds

but when I run the script, it stops after this. (EDIT) it seems to close every app besides the microsoft applications

Both opening and closing the applications scripts work great separately but I don't seem to know how to join them. If anyone knows why this is happening, that would be great. Thanks

0 投票
2 回答
188 浏览

python - 在调用 seek 后减少使用 'enumerate' 获得的可迭代计数器

我正在使用 Python 读取文件,并且在文件中有一些用“#”字符括起来的部分:

现在我编写了代码来读取文件,如下所示:

输出是:

现在您看到线路计数器lino不再有效,因为我正在使用seek. 此外,在中断循环之前减少它也无济于事,因为每次调用next. 那么在 Python 3.x 中有没有一种优雅的方法来解决这个问题呢?另外,有没有更好的方法来解决StopIteration而不在块中放置pass语句Except

更新

到目前为止,我已经根据@Dunes 的建议采用了一个实现。我不得不稍微改变一下,这样我就可以提前看看是否有新的部分开始了。我不知道是否有更好的方法来做到这一点,所以请加入评论:

类枚举文件:

然后读取文件并对每个部分进行如下处理:

0 投票
1 回答
147 浏览

applescript - 使用 Applescript 隐藏和取消隐藏单个文件

我需要编写一个applescript来隐藏和取消隐藏特定文件。这是我到目前为止所拥有的,但显然文件名中的空格会导致问题。

不知道我做错了什么。在此先感谢您的帮助。

0 投票
1 回答
316 浏览

python - 跟踪不断更新的日志文件的 Python 问题

我正在尝试跟踪另一个正在运行的程序正在创建的日志文件。如果在更新的日志中找到单词“day”(这意味着其他代码已成功运行),我想返回 True。问题是 - 有时其他程序不成功,只是卡在同一行,如果发生这种情况,我想返回 false。我尝试使用 seek() 和 tell() 跟踪它是否卡在同一行,如下所示:

但这总是为 i 和 i_old 返回相同的东西,并且当日志确实仍在更新时总是返回 false (我可能没有正确执行,我是新手)。当我尝试跟踪文件的大小(使用 os.stat)时,它也返回了相同的内容。

我也试过用

当其他程序运行得非常快但似乎有点超时并在代码运行时间过长时返回 false 时,这很有效。

我对为什么找不到一致的解决方案感到有些困惑。跟踪文件的大小或行甚至时间差似乎是一个好主意,但我似乎无法实现它。我想找到最简单的解决方案,而无需下载我在其他帖子中出现的看门狗之类的东西。任何帮助,将不胜感激!谢谢。