I have to write a list of prefixes with their occurrence count into a new text file, as long as the prefixes occur more than 1000 times.
I came up with this code, but somehow it does not work.
output = prefix_list(file_name)
file = "testfile.txt"
for values in output:
if (values[1] >= 1000):
file.write(values[0] " " int(values[1]) "\n")
Can somebody help me with what I am doing wrong here?
CodePudding user response:
The problem with your code is that you seem to don't know how to read a file.
If you want to read a file with Python, you have to use the open built-in function, this way:
with open('file.txt', 'r ') as file: #'r ' stands for read
text = file.read()
Then I would count this way the 'prefixes':
for i in text:
if (text.count(i) >= 1000):
# Do something
