Using and input string to find a tuple in a text file - python-3.x

This is my code so far. I have tried to get an input from the user to search for a tuple of 3 parts in my source code. But I can not figure out how to allow the user to find the code through input and give them the tuple. If the the name variable is changed to 'Fred' it is found with ease but the code needs for the users input. All help is much appreciated.
def details():
countries = []
file = open('file.txt', mode='r', encoding='utf-8')
file.readline()
for line in file:
parts = line.strip().split(',')
country = Country(parts[0], parts[1], parts[2])
countries.append(country)
file.close()
with open('file.txt', encoding='utf=8') as countries:
for country in countries:
name=str(input('enter:'))
if str(name) in country:
print(country)

Related

How to split strings from .txt file into a list, sorted from A-Z without duplicates?

For instance, the .txt file includes 2 lines, separated by commas:
John, George, Tom
Mark, James, Tom,
Output should be:
[George, James, John, Mark, Tom]
The following will create the list and store each item as a string.
def test(path):
filename = path
with open(filename) as f:
f = f.read()
f_list = f.split('\n')
for i in f_list:
if i == '':
f_list.remove(i)
res1 = []
for i in f_list:
res1.append(i.split(', '))
res2 = []
for i in res1:
res2 += i
res3 = [i.strip(',') for i in res2]
for i in res3:
if res3.count(i) != 1:
res3.remove(i)
res3.sort()
return res3
print(test('location/of/file.txt'))
Output:
['George', 'James', 'John', 'Mark', 'Tom']
Your file opening is fine, although the 'r' is redundant since that's the default. You claim it's not, but it is. Read the documentation.
You have not described what task is so I have no idea what's going on there. I will assume that it is correct.
Rather than populating a list and doing a membership test on every iteration - which is O(n^2) in time - can you think of a different data structure that guarantees uniqueness? Google will be your friend here. Once you discover this data structure, you will not have to perform membership checks at all. You seem to be struggling with this concept; the answer is a set.
The input data format is not rigorously defined. Separators may be commas or commas with trailing spaces, and may appear (or not) at the end of the line. Consider making an appropriate regular expression and using its splitting feature to split individual lines, though normal splitting and stripping may be easier to start.
In the following example code, I've:
ignored task since you've said that that's fine;
separated actual parsing of file content from parsing of in-memory content to demonstrate the function without a file;
used a set comprehension to store unique results of all split lines; and
used a generator to sorted that drops empty strings.
from io import StringIO
from typing import TextIO, List
def parse(f: TextIO) -> List[str]:
words = {
word.strip()
for line in f
for word in line.split(',')
}
return sorted(
word for word in words if word != ''
)
def parse_file(filename: str) -> List[str]:
with open(filename) as f:
return parse(f)
def test():
f = StringIO('John, George , Tom\nMark, James, Tom, ')
words = parse(f)
assert words == [
'George', 'James', 'John', 'Mark', 'Tom',
]
f = StringIO(' Han Solo, Boba Fet \n')
words = parse(f)
assert words == [
'Boba Fet', 'Han Solo',
]
if __name__ == '__main__':
test()
I came up with a very simple solution if anyone will need:
lines = x.read().split()
lines.sort()
new_list = []
[new_list.append(word) for word in lines if word not in new_list]
return new_list
with open("text.txt", "r") as fl:
list_ = set()
for line in fl.readlines():
line = line.strip("\n")
line = line.split(",")
[list_.add(_) for _ in line if _ != '']
print(list_)
I think that you missed a comma after Jim in the first line.
You can avoid the use of a loop by using split property :
content=file.read()
my_list=content.split(",")
to delete the occurence in your list you can transform it to set :
my_list=list(set(my_list))
then you can sort it using sorted
so the finale code :
with open("file.txt", "r") as file :
content=file.read()
my_list=content.replace("\n","").replace(" ", "").split(",")
result=sorted(list(set(my_list)))
you can add a key to your sort function

How to append data to CSV file using pandas and User Input?

I've posted about this before using import csv and couldn't figure it out, but now I've decided to step it "up a notch" and use pandas instead. When I try to add data using User Input, I cannot figure out why it won't update. I attempted to look up some solutions online but can't find any. I will add my code and then a photo of what the CSV file looks like and another photo of what the actual program looks like when information is entered into the command line . What I'm trying to accomplish is have a new Name, Location, and Email added at the end of the data(CSV file) and then saved. But nothing happens when it is entered.
import pandas as pd
dataset = pd.read_csv(r'C:\Users\benji\OneDrive\Desktop\My Coding Work\.vscode\.vscode\CSVFiles\emails_demo.csv')
names = list(dataset.Name)
locations = list(dataset.Location)
emails = list(dataset.Email)
def get_email_by_loc(location):
correct_emails = list(dataset.loc[dataset.Location == location].Email)
return correct_emails
def add_data():
global names
global locations
global emails
global dataset
add_name = input('Enter Name: ')
add_location = input('Enter Location: ')
add_email = input('Enter Email: ')
names.append(add_name)
locations.append(add_location)
emails.append(add_email)
while True:
start_search = input('What would you like to search?(Name/Location/Email/Add Data): ')
if start_search == 'Location' or start_search == 'location':
location_search = input('Enter Location: ')
for emails in get_email_by_loc(location_search):
print(emails)
if start_search == 'Add Data' or start_search == 'add data':
add_data()
CSV: https://imgur.com/a/ziESanM
Command Line: https://imgur.com/a/UDRoiDj
names = list(dataset.Name)
locations = list(dataset.Location)
emails = list(dataset.Email)
returns list that are not associated with the original dataframe (ie you are appending to arrays that are not even associated with the original dataframe) so when you are going to rewrite to the csv file, you are not appending to the original dataframe. I would recommend fixing the add_data function to append to the end of the dataframe.
One quick (but no where close to ideal) version to do so would be
def add_data():
global dataset
add_name = input('Enter Name: ')
add_location = input('Enter Location: ')
add_email = input('Enter Email: ')
dataset.loc[len(dataset)]=[add_name,add_location,add_email ]
If you would like to write to the original csv file, you may use dataset.to_csv(file_path)
This is more ideal, let me know if it works

Reading a .txt file and appending each word into a dictionary

I'm kind of on a time crunch, but this was one of my problems in my homework assignment. I am stuck, and I don't know what to do or how to proceed.
Our assignment was to open various text files and within each of the text files, we are supposed to add each word into a dictionary in which the key is the document number it came from, and the value is the word.
For example, one text file would be:
1
Hello, how are you?
I am fine and you?
Each of the text files begin with a number corresponding to it's title (for example, "document1.txt" begins with "1", "document2.txt" begins with "2", etc)
My teacher gave us this coding to help with stripping the punctuation and the lines, but I am having a hard time figuring out where to implement it.
data = re.split("[ .,:;!?\s\b]+|[\r\n]+", line)
data = filter(None, data)
I don't really understand where the filter(None, data) stuff comes into play, because all it does is return a code line of what it represents in memory.
Here's my code so far:
def invertFile(list_of_file_names):
import re
diction = {}
emplist = []
fordiction = []
for x in list_of_file_names:
afile = open(x, 'r')
with afile as f:
for line in f:
savedSort = filterText(f)
def filterText(line):
import re
word_delimiters = [' ', ',', ';', ':', '.','?','!']
data = re.split("[ .,:;!?\s\b]+|[\r\n]+", f)
key, value = data[0], data[1:]
diction[key] = value
How do I make it so each word is appended into a dictionary, where the key is the document it comes from, and the value are the words in the document? Thank you.

How to extract information from a text file that is located on a web page in python

I am a total beginner and I'm trying to do the following. I need to open a text file from a web page which contains a small list like that below.
name lastname M 0909
name lastname C 0909
name lastname F 0909
name lastname M 0909
name lastname M 0909
What I need to do is to count how many big M letters and how many big different letters there is(here is 3 M,F and C)and print it out. Then I need to create a new text file and transfer (only) all the names into it and save it on my hard drive. So far I only figured out how to open the list from web page.
import urllib.request
url = 'http://mypage.com/python/textfile.txt'
with urllib.request.urlopen(url) as myfile:
for i in myfile:
i = i.decode("ISO-8859-1")
print(i,end=" ")
But that is all I know. I tried using count() but it counts only one line at the time, it counts how many big M letters are in one line(1) but it does not add them together for the whole text(3). Any help would be appreciated, thank you.
I don't know exactly what you are doing, but try this:
import urllib.request
url = 'http://mypage.com/python/textfile.txt'
with urllib.request.urlopen(url) as myfile:
number_of_M = 0
set_of_big_letters = set()
for i in myfile:
i = i.decode("ISO-8859-1")
name, lastname, big_letter, _ = i.split(' ') # if they are seperated by space
set_of_big_letters.add(big_letter)
if big_letter == 'M':
number_of_M += 1
print(number_of_M)
print(len(set_of_big_letters))

text file reading and writing, ValueError: need more than 1 value to unpack

I need to make a program in a single def that opens a text file 'grades' where first, last and grade are separated by comas. Each line is a separate student. Then it displays students and grades as well as class average. Then goes on to add another student and grade and saves it to the text file while including the old students.
I guess I just don't understand the way python goes through the text file. If i comment out 'lines' I see it prints the old_names but its as if everything is gone after. When lines is not commented out 'old_names' is not printed which makes me think the file is closed? or empty? however everything is still in the txt file as it should be.
currently i get this error.... Which I am pretty sure is telling me I'm retarded there's no information in 'line'
File "D:\Dropbox\Dropbox\1Python\Batch Processinga\grades.py", line 45, in main
first_name[i], last_name[i], grades[i] = line.split(',')
ValueError: need more than 1 value to unpack
End goal is to get it to give me the current student names and grades, average. Then add one student, save that student and grade to file. Then be able to pull the file back up with all the students including the new one and do it all over again.
I apologize for being a nub.
def main():
#Declare variables
#List of strings: first_name, last_name
first_name = []
last_name = []
#List of floats: grades
grades = []
#Float grade_avg, new_grade
grade_avg = new_grade = 0.0
#string new_student
new_student = ''
#Intro
print("Program displays information from a text file to")
print("display student first name, last name, grade and")
print("class average then allows user to enter another")
print("student.\t")
#Open file “grades.txt” for reading
infile = open("grades.txt","r")
lines = infile.readlines()
old_names = infile.read()
print(old_names)
#Write for loop for each line creating a list
for i in len(lines):
#read in line
line = infile.readline()
#Split data
first_name[i], last_name[i], grades[i] = line.split(',')
#convert grades to floats
grades[i] = float(grades[i])
print(first_name, last_name, grades)
#close the file
infile.close()
#perform calculations for average
grade_avg = float(sum(grades)/len(grades))
#display results
print("Name\t\t Grade")
print("----------------------")
for n in range(5):
print(first_name[n], last_name[n], "\t", grades[n])
print('')
print('Average Grade:\t% 0.1f'%grade_avg)
#Prompt user for input of new student and grade
new_student = input('Please enter the First and Last name of new student:\n').title()
new_grade = eval(input("Please enter {}'s grade:".format(new_student)))
#Write new student and grade to grades.txt in same format as other records
new_student = new_student.split()
new_student = str(new_student[1] + ',' + new_student[0] + ',' + str(new_grade))
outfile = open("grades.txt","w")
print(old_names, new_student ,file=outfile)
outfile.close()enter code here
File objects in Python have a "file pointer", which keeps track of what data you've already read from the file. It uses this to know where to start looking when you call read or readline or readlines. Calling readlines moves the file pointer all the way to the end of the file; subsequent read calls will return an empty string. This explains why you're getting a ValueError on the line.split(',') line. line is an empty string, so line.split(",") returns a list of length 0, but you need a list of length 3 to do the triple assignment you're attempting.
Once you get the lines list, you don't need to interact with the infile object any more. You already have all the lines; you may as well simply iterate through them directly.
#Write for loop for each line creating a list
for line in lines:
columns = line.split(",")
first_name.append(columns[0])
last_name.append(columns[1])
grades.append(float(columns[2]))
Note that I'm using append instead of listName[i] = whatever. This is necessary because Python lists will not automatically resize themselves when you try to assign to an index that doesn't exist yet; you'll just get an IndexError. append, on the other hand, will resize the list as desired.

Resources