How do I count unique names? - python

I am trying to count up unique names that start with "From:" from a file name. However, I keep getting a long list of numbers. What is my code actually reading and how do I fix this?
count = 0
name = []
fname = input("What is your file name? Enter it here: ")
try:
fname = open(fname)
name = set(f.readlines())
except:
print ("That file does not exist.")
for name in fname:
if name.startswith("From:"):
count = len(name)
print (count)

We can make use of set to hold all required names and find its length to get the count:
file_name = input("What is your file name? Enter it here: ")
s = set()
with open(file_name) as f:
for name in f:
if name.startswith('From:'):
s.add(name)
print(len(s))

Try this:
words = []
count = 0
with open ("unique.txt","r") as f:
# Get a list of lines in the file and covert it into a set
words = set(f.readlines())
FromWords=[]
for word in words:
if word.startswith("From:"):
FromWords.append(word)
print(len(FromWords))
First, we filter out all duplicate words and then look for the words which start with From: and this may aid in faster processing if you're dealing with the big amount of data.
let me know if you need any help in this regard.

Related

Nested list missing after read from file

So, i have a nested list written into a file.txt
students = []
info = []
name = input("Name: ")
age = input("Age: ")
info.append(name)
info.append(age)
students.append(info)
my_file = open("file.txt", "a")
for data in students:
my_file.write("%s\n" % data)
my_file.close()
The contents in the file are in this format:
['john', '19']
['nick', '20']
Afterwards, i'm using nested loop to access the content of file.txt
my_file = open("file.txt", "r")
search_keyword = input("Please Enter Student Name: ")
for students in my_file:
for info in students:
print(info)
Expected output:
john
19
nick
20
Actual output:
j
o
h
n
1
9
n
i
c
k
2
0
Can someone explain why the inner list is missing after extracting from a file, as the loop treats each individual alphabet as an element.
Uh, I'm not 100% sure but maybe try: my_file.writelines("")
This seems a good candidate for pickle since you have a Python data structure you want to preserve.
import pickle
lol = [['john', '19'],['nick', '20']]
with open('lol.pickle','wb') as f:
pickle.dump(lol,f)
del lol
print(lol)
# NameError: name 'lol' is not defined
with open('lol.pickle','rb') as f:
lol = pickle.load(f)
print(lol)
# [['john', '19'], ['nick', '20']]
You have read the data as a string, so when you iterate you are iterating through elements of a string, not elements of a list.
I am assuming that you are learning python. The current program works, but is not the best way to do it. You should try using csv or pickle. However, it is always good to start from basic! :D
students = []
info = []
name = input("Name: ")
age = input("Age: ")
info.append(name)
info.append(age)
students.append(info)
with open("file.txt", "a") as my_file:
for data in students:
my_file.write("%s,%s\n" % (data[0], data[1])) # this will write in file like name,age
After that you can retrieve like
search_keyword = input("Please Enter Student Name: ")
with open("file.txt", "r") as my_file:
for students in my_file:
for info in students.split(','): # We split it based on commas to get the desired output
print(info)
The mistake you were making is that when you tried for info in students you were iterating over characters in the string rather than the actual information.
Notice how we have used with open this will do all the file handling automatically.
Iterating over file-like objects produces lines
for students in my_file:
On each iteration students will be a line of text.
Iterating over text produces individual characters.
for info in students:
On each iteration info will be a character.
If your text files have string representations of python objects you can use ast.literal_eval to evaluate the objects.
import ast
with open('file.txt') as f:
for line in f:
thing = ast.literal_eval(line)
print(thing)
for item in thing:
print(item)
As mentioned in the docs, ast.literal_eval Safely evaluate an expression node or a string with emphasis on safe - it shouldn't evaluate destructive or unwanted Python statements.
You would be better off using one of the built-in data persistence modules or perhaps json or even xml to save your data.

Can't read numbers from a text file in python

I have to read a list of 12 grades from a text file. Each grade is on a new line in the text file. I have to find the lowest grade and drop it(in the text file as well), then find the average score of the grades. I am having trouble making the text values into integer values so I can find the average. I also can't figure out how to find the lowest grade and drop it.
Here is my code:
try:
homeworkFile = open('homework.txt', 'r')
except:
print("Error: invalid file")
else:
lines = homeworkFile.readlines()
homeworkFile.close()
homeworkFile = open('homework.txt', 'w')
for line in lines:
Thanks for any help you can give!
So this is just one way to take all of your values and calculate the average.
input_file = input("enter file name: ")
open_file = open(input_file, 'r')
Here I just put all of your values into notepad and read through it
grades_list = []
for grade in open_file:
grade_format = grade.strip() #how to remove extra blank space
grades_list.append(grade_format)
Then I used a for-loop to go through each line and put the grades into a list
grade_convert = [] #new list for all of the converted values
for grade in grades_list:
convert = float(grade) #convert each value in the list into a float
grade_convert.append(convert)
I used another list for converted each value
grade_convert = sorted(grade_convert) #very first element will be the lowest
grade_convert.pop(0) #permanently removes the very first item
grade_total = 0 #set a counter
for grade in grade_convert:
grade_total += grade #add all of the values to the counter
grade_average = grade_total / len(grade_convert) #len is number of items
print(grade_average)
grades_list = []
for line in lines :`
grade = float(line)
grades_list.append(grade)
grades_list.sort()
lowest_grade = grades_list[0] #There is the lowest grade
This is one way you can structure your logic. You need to convert values to float pre-processing. Using with is recommended instead of using open and close explicitly.
import os
file = 'homework.txt'
# check if file exists
assert os.path.exists(file), "File does not exist: {0}".format(file)
# use with to open/close file implicitly
with open(mystr, 'w') as file_in:
lines = homeworkFile.readlines()
grades = sorted(float(line) for line in lines)
# drop lowest trade
del grades[0]

How do I find all unique words(no duplicates)?

I would like to find all the unique words that are in both files. I am able to list all the words from each file but it gives me duplicates. I also would like to sort them by alphabetical order. How do I go about doing this?
#!/usr/bin/python3
#First file
file = raw_input("Please enter the name of the first file: ")
store = open(file)
new = store.read()
#Second file
file2 = raw_input("Please enter the name of the second file: ")
store2 = open(file2)
new2 = store2.read()
for line in new.split():
if line in new2:
print line
Here is a snippet which might help you:
new = 'this is a bunch of words'
new2 = 'this is another bunch of words'
unique_words = set(new.split())
unique_words.update(new2.split())
sorted_unique_words = sorted(list(unique_words))
print('\n'.join(sorted_unique_words))
Update:
If you're only interested in words that are common to both files, do this instead:
unique_words = set(new.split())
unique_words2 = set(new2.split())
common_words = set.intersection(unique_words, unique_words2)
print('\n'.join(sorted(common_words)))

Printing specific text from a line in python

I'm writing a mini diceware program designed to take inputs from the users with real dice, look up the values, and print out their diceware passphrase.
The code I have at the moment works fine and pulls the number and words from a wordlist by searching for the 5-digit diceware identifier (e.g. 34465 jilt).
But, I'm hoping to make the pass phrase print as one line without the number association. e.g. as
"jilt load re open snap" instead of
34465 jilt
load
etc.
At the moment this is that code I'm using:
p_length = int(raw_input("How many dicewords?"))
roll_list = []
for i in range(p_length):
seq1 = (raw_input("roll 1:"), raw_input("roll 2:"),
raw_input("roll 3:"), raw_input("roll 4:"),
raw_input("roll 5:"))
str1 = "".join(seq1)
roll_list.append(str1)
print roll_list
with open("name_list.txt") as f:
for line in f:
for x in roll_list:
if x in line:
print line
Any suggestions on how to change the last few lines there to do what I'm hoping?
Thanks for the split() advice. Here is my solution:
passphrases = []
with open("name_list.txt") as f:
for line in f:
for x in roll_list:
if x in line:
var1 = line.split(" ")
var2 = var1.pop( )
passphrases.append(var2.rstrip('\n'))
print " ".join(passphrases)
You can use any here.Updated for just fixing.
for line in f:
if any(i for i in roll_list if i in line.strip()):
print line.strip().split(" ")[-1]
>>>hold romeo
You can use split to break up a line and extract just the word w/o the number.

use a function on every item in a list python

Hello I am trying to build a tool that will compress a list of folders and rename the compressed file, this list of the names of folders I want to compress are located in a .txt file, the .txt is something like this:
james, 5005
kyle, 02939
Betty, 40234
I have used multiple methods to try and build this code but I keep getting a python error set object is not subscriptable and I have no idea what to do to rectify this and on how to continue from here. Can I not use shutil.make_archive with dictionaries or can I use lists? because I would like to run this function down the first column and to rename the files i am creating using the second column. I am using python 3, and any help would be great!
import os
import shutil
x = input("Input Path your user list: ")
filename = input("Input user file name: ")
changedir = input("Input where your folders are: ")
os.chdir(changedir)
userfile = x + filename + ".txt"
print("Awesome your path is now", userfile)
with open(userfile, "rt") as userfileone:
count = 0
while 1:
buffer = userfileone.read(8192*1024)
if not buffer: break
count += buffer.count('\n')
print("It is indicated that there are", count + 1, "in the file")
with open(userfile, "rt") as f:
lines = f.readlines()
dic = {}
for x in lines:
x = x.strip().split(',')
dic[x[0]]=tuple(x[1:])
for i in dic:
shutil.make_archive(i, "zip", dic[i])
It seems like you are looking for the map function.

Categories

Resources