English 中文(简体)
A. 档案内容的改变——Sharma
原标题:Changing contents of a file - Python
  • 时间:2012-04-10 15:15:48
  •  标签:
  • python

因此,我有一个方案。 这是法典的一部分:

FileName =  Numberdata.dat 
NumberFile = open(FileName,  r )
for Line in NumberFile:
  if Line ==  4 :
    print( 1 )
  else:
    print( 9 )
NumberFile.close()

做一件事,是的,但我只是为了增进我的理解而做的。 然而,这部法律并没有奏效。 档案依然如此,4个文件没有被1个取代,其他所有文件都用9个字取代,它们只是保留了。 在什么地方我错了?

编号:444666444666444888111000444>

现在是:

FileName =  Binarydata.dat 
BinaryFile = open(FileName,  w )
for character in BinaryFile:
  if charcter ==  0 :
    NumberFile.write(  )
  else:
    NumberFile.write( @ )
BinaryFile.close()
最佳回答

你们需要建构,并写到档案中。

FileName =  Numberdata.dat 
NumberFileHandle = open(FileName,  r )
newFileString = ""
for Line in NumberFileHandle:
  for char in line: # this will work for any number of lines.
      if char ==  4 :
        newFileString += "1"
      elif char ==  
 :
        newFileString += char
      else:
        newFileString += "9"
NumberFileHandle.close()

NumberFileHandle = open(FileName,  w )
NumberFileHandle.write(newFileString)
NumberFileHandle.close()
问题回答

首先,Line 将永远不等于4,因为从档案中读出的每一条线都包含最终的新线特性。 Try if Line.strip() = 4 这将把所有白色空间从线一开始和终点移走。

Edit:我亲眼看到你的话......自然,如果你在一线上有你的所有数字,这个线永远不会平等。 4. 第四条第4款 你可能想在某一时间读到档案,而不是一行。

第二,你没有写到任何档案中,因此,档案自然会变换。 你将难以改变一个文件,因为你必须说明如何支持你刚才读到的同一地点,所以通常的做法是从一个文件读到另一个文件。

因为你也需要写到档案中。

with open(FileName,  w ) as f:
    f.write(...)

Right now you are just reading and manipulating the data, but you re not writing them back.

最后,你必须重开书面文件并写给它。

查阅参考资料的请参看http://docs.python.org/py3k/library/Functions.html#open"rel=“nofollow” 文件Reading和Documents/pdocs/folk/

<><>Edit>: 页: 1 您可以写到温文档和结尾处shutil.move(>,或装载和操纵你的数据,然后以书面方式打开其原始档案并写回。

你没有向数据发送任何产出,你只是印刷<代码>19stdout,通常都是终端或口译。

If you want to write to the file you have to use open again with w. eg.

out = open(FileName,  w )

也可以使用

print >>out,  1 

然后请上<代码>out.write ( 1 ),例如。

同样,如果你想在之后做超礼和书写的话,最好先读文件。

According to your comment:

数字数据只是所有一条线的编号。 难道这错了吗? “444666444666444888111000444”

我可以告诉你,<<的周期>,在上,而不是在>上。 逻辑错误。

此外,正如Rik Poggi所说(以书面方式打开公证假)。

Because print doesn t write to your file. You have to open the file and read it, modify the string you obtain creating a new string, open again the file and write it again.

FileName =  Numberdata.dat 
NumberFile = open(FileName,  r )
data = NumberFile.read()
NumberFile.close()
dl = data.split( 
 )
for i in range(len(dl)):
    if dl[i] == 4 :
        dl[i] =  1 
    else:
        dl[i] =  9 
NumberFile = open(FileName,  w )
NumberFile.write( 
 .join(dl))
NumberFile.close()

这样做。 当然,有不同的方法,但这似乎属于我=的最“线”方法。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签