用python写一个文件

时间:2012-07-28 21:00:55

标签: python

这是我昨天提出的问题的延续。我正在尝试打开包含数字列表的文本文件。我想多次将这些数字写入新文件。循环的目的是能够多次写入源列表。在这个例子中,我想打印100个数字的列表10次,所以我最终在输出文件中列出了1000个数字。这是我正在使用的代码:

i = 10
while i > 0:
    with open ('C:/TestReq_100 Records.txt', 'r') as ipf:
       for line in ipf:
            num = line.strip()
            filename = 'processed.txt'
            with open('processed.txt', 'w') as opf:
                opf('%s' %num)


##           print num
    i = i - 1

如果我注释掉与写入文件相关的代码并在解释器中使用print命令,那么代码就是我想要的。我似乎无法在文本文件中获得相同的输出。再说一遍,我不是学生。只是尝试创建文件以供我公司的软件用于测试目的...谢谢!

4 个答案:

答案 0 :(得分:2)

你没有准确说明输出中的问题,但试试这个:

 opf.write('%s\n' %num)

而不是

 opf('%s' %num)

备注

  1. 您正在输出文件对象
  2. 上调用write方法
  3. 您要在输出中添加换行符\nprint默认情况下执行此操作,此时应该使您的打印件与此等效。
  4. 你正在打开和相同的文件一遍又一遍地写信给你,这是你的意图吗?您只会保留最后写入该文件的内容。您的帖子中有一些更多详细信息(关于输出的具体问题,您给定的输入和所需的输出将使您更容易帮助您实现目标)

    更新基于以下评论中的更多信息。

    按照这些方式构建您的程序(虽然确切的顺序有点 iffy ,但由于缺乏明确的规范,您可以将其作为指南):

    with open ('C:/TestReq_100 Records.txt') as ipf:
        with open('processed.txt', 'w') as opf:
            i = 10
            while i > 0:
                for line in ipf:
                    num = line.strip()
                    filename = 'processed.txt'
                    write.opf('%s\n' %num)
    ##           print num
                i = i - 1
    

    输出文件在循环之前打开

    顺便说一句,如果您使用的是相当新版本的Python,则可以将两个with语句合并为一个:

    with open('C:/TestReq_100 Records.txt') as ipf,open('processed.txt','w') as opf:
    

    请注意,您可以使用两个for循环,即用for循环替换当前的while循环,但这更多的是样式和语义问题,它不会影响代码的功能。

答案 1 :(得分:2)

除了Levon的回答,您当前的计划还存在一些问题。

  • 不要手动初始化和递减索引,而是尝试使用xrange以更简洁的方式完成相同的任务。

  • 您正在覆盖每个号码的processed.txt;你应该在输入文件的同时打开它。

  • i的目的是什么?按照目前的情况,你的程序会做10次完全相同的事情,因为i从未在循环中使用过。

  • '%s' % num已经是字符串时,
  • num没有什么特别的。

那就是说,虽然i仍然没有做任何特别的事情,但这是你的程序的更清洁版本:

for i in xrange(10, 0, -1):
    with open('C:/TestReq_100 Records.txt', 'r') as ipf:
        with open('processed.txt', 'w') as opf:
            for line in ipf:
                num = line.strip()
                opf.write(num)

您可能希望澄清此计划的预期输出,以获得更好的答案。

编辑:这是一种将文件多次复制到新文件的更有效的解决方案:

# Read the input data once.
with open('C:/TestReq_100 Records.txt', 'r') as ipf:
    ipdata = ipf.read()
# Add a newline to the end of the file if there isn't already one present.
# Without this, the last and first lines of the input might become single lines.
if ipdata[-1] != '\n':
    ipdata += '\n'
# Write to the output file multiple times.
with open('processed.txt', 'w') as opf:
    for i in xrange(10):
        opf.write(ipdata)

答案 2 :(得分:0)

你没有说明你的问题是什么,但看起来你至少有两个问题。

首先,您在'w'模式下多次打开同一个文件。 'w'模式不会附加到现有文件,它将覆盖现有文件。所以你只能获得循环的最后一次迭代。您可以通过在循环开始之前打开文件并在结束之后关闭它来解决此问题。如果需要,您仍然可以使用with语法执行此操作。

其次,我认为您需要使用opf.write()而不仅仅是opf(),除非这是我不知道的语法。

试试这个:

with open ('C:/TestReq_100 Records.txt', 'r') as ipf:
    with open('processed.txt', 'w') as opf:
        i = 10
        while i > 0:
            for line in ipf:
                num = line.strip()
                filename = 'processed.txt'
                opf.write('%s' %num)
            i = i - 1

答案 3 :(得分:0)

如果我理解正确,应该这样做......(在2.7.x中)

from itertools import islice

MAX = 10

with open('infile') as fin, open('outfile', 'w') as fout:
    for line in islice(fin, MAX):
        print >> fout, line.strip()