gpt4 book ai didi

python - 如何使用Python从特定位置读取文件到特定位置?

转载 作者:行者123 更新时间:2023-12-01 00:14:01 25 4
gpt4 key购买 nike

目前,我正在做:

    source_noise = np.fromfile('data/noise/' + source + '_16k.dat', sep='\n')
source_noise_start = np.random.randint(
0, len(source_noise) - len(audio_array))
source_noise = source_noise[source_noise_start:
source_noise_start + len(audio_array)]

我的文件如下所示:

  -5.3302745e+02
-5.3985005e+02
-5.8963920e+02
-6.5875741e+02
-5.7371864e+02
-2.0796765e+02
2.8152341e+02
6.5398089e+02
8.6053581e+02

..等等。

这要求我读取整个文件,而我只想读取文件的一部分。有没有什么方法可以用 Python 来完成这件事,并且比我现在做的更快?

最佳答案

您可以使用seek方法在文件内移动并读取特定位置。

文件数据 -> “hello world”

start_read = 6

with open("filename", 'rb') as file:
file.seek(start_read)
output = file.read(5)
print(output)

# will display world

关于python - 如何使用Python从特定位置读取文件到特定位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59445281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com