我正试图从文本文件中提取一些数据。这些是代码行:
directory = './mydirec'
files = glob('{0:s}/*.gather.txt'.format(directory))
我一直收到[],所以没有结果。有人可以帮我理解为什么?感谢
也许我误解了你的问题,但从.txt中收集数据真的很容易。您可以打开并读取带有3或4行代码的文本文件:
f = open("file.txt", "r+")
data = f.read()
print(data)
f.close()
如果你正在从.txt的特定行中寻找数据,你可以使用:
f = open("file.txt", "r+")
lines = f.readlines()
print(lines[25])
f.close()
或者如果你想在普通中的文本文件中查找特定的数据
f = open("file.txt", "r+")
read = f.read()
data = read
f.close()
if "<specific word you're looking for>" in data:
#react based on the information being in the data.
我不认为你必须使用.format或类似的东西,你可以只使用文件的路径,或者如果它和你的脚本在同一个文件夹中,只使用文件名。