Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python在保存一行之后不断删除一行_Python_Python 2.7_Sorting_Line_Strip - Fatal编程技术网

Python在保存一行之后不断删除一行

Python在保存一行之后不断删除一行,python,python-2.7,sorting,line,strip,Python,Python 2.7,Sorting,Line,Strip,我的代码看起来像 from\uuuuu future\uuuuu导入打印功能 导入行缓存 从随机进口选择 从字符串导入数字,ascii_小写 导入随机、字符串 导入文件输入 L=''.join([random.choice(string.ascii_字母+string.digits)表示xrange(10)中的n)]) 印刷品(L) 将open(“input.txt”)作为f: 对于f中的行: ok=(行) ok=line.strip() 如果行>6: f=打开('output.txt','a

我的代码看起来像

from\uuuuu future\uuuuu导入打印功能
导入行缓存
从随机进口选择
从字符串导入数字,ascii_小写
导入随机、字符串
导入文件输入
L=''.join([random.choice(string.ascii_字母+string.digits)表示xrange(10)中的n)])
印刷品(L)
将open(“input.txt”)作为f:
对于f中的行:
ok=(行)
ok=line.strip()
如果行>6:
f=打开('output.txt','a')
f、 写入(str(ok)+“”+str(L)+'\n')
f、 关闭()
总计数=0
对于fileinput.input('output.txt',inplace=True)中的行:
计数=行。计数(确定)
如果计数>0:
打印(行,结束=“”)
总计数+=计数
打印(总计数)
该模块可能会导致正确写入文件输出出现问题,但我不确定该模块如何工作,因此无法建议如何使用它

主要的修正可能是使用不同的文件处理程序指针来读取输入文件和写入输出文件(不要重复使用注释中提到的
f

以下是对原始代码的一些重构:

from __future__ import print_function
import linecache
from random import choice
from string import digits, ascii_lowercase
import random, string
from collections import defaultdict

L = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(10)])
print(L)

total_count = defaultdict(lambda: 0)

with open("input.txt", 'r') as f:
    for line in f:
        ok = line.strip()
        if int(line) > 6:
            total_count[int(line)] += 1
            with open('output.txt', 'a' ) as ff:
                ff.write( str(ok) + " " + str(L) + '\n')

infile_contents = []
outfile_contents = []

# get list of input.txt contents
with open('input.txt', 'r') as infile:
    for line in infile.readlines():
        infile_contents.append(line.strip())

# get list of output.txt first column contents
with open('output.txt', 'r') as outfile:
    for line in outfile.readlines():
        outfile_contents.append(line.split()[0])        

# dictionary to hold count of occurrences
totals = {}

# count occurrences
for num in infile_contents:
    totals[num] = outfile_contents.count(num)

# print counts
for item in totals.items():
    print('{num} appears {count} times.'.format(num=item[0], count=item[1]))
运行脚本两次后的输出示例:

vlVHVi3t9L
55 appears 2 times.
99 appears 2 times.
42 appears 2 times.
65 appears 2 times.
49 appears 4 times.

如果第6行:出现错误,因为您正在将字符串(第一个输入看起来像“55”)与整数(6)进行比较。您在
f=open('output.txt',a')处覆盖了
f
它没有给我一个错误,我将f改为ff,我得到了相同的结果。代码中的最后一个循环是:
对于fileinput.input('output.txt',inplace=True):
可能是问题的原因。尝试将整个块的缩进(从fileinput.input…中的行的
移到
打印(总计数)
)的末尾,移回左边距(与打开的
行(“input.txt”)对齐,如f:
),然后再次运行脚本。@davedwards它现在成功地保存了行,但只保存了最后两行,每次我运行它时,它都会继续添加这两行,它们是“49”。这段代码不断删除输出文件的内容,只是为了保存新行,似乎只计算有多少行,不是输入中的一行在输出中出现多少次示例是42在输出中出现两次。txt
42
应该只在output.txt文件中出现一次(请参见上面答案中的示例输出,只有一个
42
条目)。你是说出现两次的
49
应该只算一次
吗?这里的问题是,如果我运行代码3次,在保存输出文件时删除了它的所有内容,42应该在输出文件中出现3次,应该在python shellAh中打印,好的,我删除了删除output.txt内容的行。我将尝试修复它,以计算文件内容的出现次数。请在答案中输入新代码,如果没有帮助,请提供反馈。谢谢