对于我正在进行的练习,我正在尝试使用该read()方法两次读取给定文件的内容.奇怪的是,当我第二次调用它时,它似乎没有将文件内容作为字符串返回?
这是代码
f = f.open()
# get the year
match = re.search(r'Popularity in (\d+)', f.read())
if match:
print match.group(1)
# get all the names
matches = re.findall(r'<td>(\d+)</td><td>(\w+)</td><td>(\w+)</td>', f.read())
if matches:
# matches is always None
Run Code Online (Sandbox Code Playgroud)
当然我知道这不是最有效或最好的方式,这不是重点.关键是,为什么我不能read()两次打电话?我是否必须重置文件句柄?或者关闭/重新打开文件以执行此操作?
我也试过寻找答案,但我不明白其他人类似问题的答案......
tfile= open("/home/path/to/file",'r')
def temp_sky(lreq, breq):
for line in tfile:
data = line.split()
if ( abs(float(data[0]) - lreq) <= 0.1
and abs(float(data[1]) - breq) <= 0.1):
T= data[2]
return T
print temp_sky(60, 60)
print temp_sky(10, -10)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
7.37052488
Traceback (most recent call last):
File "tsky.py", line 25, in <module>
print temp_sky(10, -10)
File "tsky.py", line 22, in temp_sky
return T
UnboundLocalError: local variable 'T' referenced before assignment
Run Code Online (Sandbox Code Playgroud)
第一个print语句正常工作,但第二个不起作用.我尝试将T设为全局变量,但这使得两个答案都相同!请帮忙!
我正在编写一个程序,要求我多次遍历文件的每一行:
loops = 0
file = open("somefile.txt")
while loops < 5:
for line in file:
print(line)
loops = loops + 1
Run Code Online (Sandbox Code Playgroud)
为了简洁起见,我假设我总是需要遍历文件并打印每行5次.该代码与我在程序中实现的较长版本具有相同的问题:文件只迭代一次.之后,print(line)文件什么都不做.为什么是这样?
我对 Python 很陌生。我想处理现有文件 ( exist_file),并创建它的副本。问题是,当我创建文件的副本时,它exist_file变成空的。
exist_file = open('some_pass/my_file.txt', 'r')
print exist_file.read() # Here the file is successfully printed
copy_of_file = open('new_copied_file.txt', 'w')
copy_of_file.write(exist_file.read())
print exist_file.read() # Here the file is empty
Run Code Online (Sandbox Code Playgroud)
为什么exist_file是空的?
我有一个文件,其中有一些名称逐行列出.
gparasha-macOS:python_scripting gparasha$ cat topology_list.txt
First-Topology
Third-topology
Second-Topology
Run Code Online (Sandbox Code Playgroud)
现在我试图迭代这些内容,但我无法这样做.
file = open('topology_list.txt','r')
print file.readlines()
for i in file.readlines():
print "Entered For\n"
print i
topology_list = file.readlines()
print topology_list
Run Code Online (Sandbox Code Playgroud)
file.readlines()以列表形式打印文件的行.所以我得到了这个:
['First-Topology\n', 'Third-topology\n', 'Second-Topology\n']
Run Code Online (Sandbox Code Playgroud)
但是,当我遍历此列表时,我无法这样做.
此外,当我将它分配给变量'topology_list'时,如倒数第二行并打印它.它给了我一个空列表.
[]
Run Code Online (Sandbox Code Playgroud)
所以我有两个问题.
我的做法有什么问题?怎么做到这一点?
我有日志文件(以YYMMDD格式命名),我想创建一个脚本,只从文件中获取重要信息(如包含"O:NVS:VOICE"的行).我之前从未使用过Python,所以请帮忙!
当然,这个问题以前已经被问过,但我找不到这个问题。
据我了解,在Python中(我使用的是3.3,但这对2.x和3.x来说是通用的)你不能在打开的文本文件上多次迭代,这是因为光标被移动到末尾并且不会返回到下一个可迭代循环的开头。因此它的行为不像一个更典型的可迭代对象。
我想知道如何将光标返回到开头,或者至少在要读取的打开文件上连续两个 for 循环。
谢谢。
目前,这段代码使我从头到尾都掌握了所有内容searchfile。但是根据我的新手理解,它应该从searchfile每一infile行中打印出所有行!
searchfile = open('.\sometext.txt', 'r')
infile = open('.\somefile', 'r')
for line1 in infile:
for line2 in searchfile:
print line2
searchfile.close()
infile.close()
Run Code Online (Sandbox Code Playgroud)
我试图用来searchfile.readlines()创建一个列表,以打印所有infile行的所有searchfile行,但仍然无法正常工作。有人有线索吗?
我编写了一个小类,它读取文本文件,并且有一个打印文本的方法(file.output())。对于第一次调用它有效,但第二次调用该方法没有任何反应。我不明白为什么,因为我认为 FOR 循环不会改变任何东西。
class Datei():
def __init__(self, filename):
self.fileobject = open(filename)
def output(self):
for line in self.fileobject:
print(line.rstrip())
def end(self):
self.fileobject.close()
file = Datei("yellow_snow.txt")
file.output()
print("second try")
file.output()
file.end()
Run Code Online (Sandbox Code Playgroud)
我预计文本文件的文本会打印两次,但它只打印一次。
我是Python初学者,正在用较小的独立文件对我的较大项目进行故障排除。我的较大项目的问题是我无法让两个for循环连续工作。
我有一个包含两行的txt文件
代码很简单:
file=open("test.txt","r")
for line in file:
print("x")
pass
print("hi")
for line in file:
print("x")
Run Code Online (Sandbox Code Playgroud)
两个for循环是相同的bar pass。
print("hi") 作为检查第一个for循环是否中断的手段而存在
此代码输出:
x
x
x
hi
Run Code Online (Sandbox Code Playgroud)
打破了第一个for循环,并且print语句在其后起作用,但是那之后的for循环不是吗?我本来希望它输出:
x
x
x
hi
x
x
x
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
谢谢