I'm struggling to add those numbers together and put names apart.
So, I need to print each line containing names and total numbers.
e.g. Peter Jones: 155
File 'test1.txt' example:
Marshall Rogers, 88, 21, 90
Richard Lao, 30
Peter Jones, 23, 54,78
AABB CC EE RR rest, 90, 3, 3, 4
Here's my code:
def find_their_numbers(files):
"""print it out"""
file = open(files)
lines = file.read().splitlines()
nam = ""
new_list = []
for name in lines:
names = name.split(',')
for i in range(len(names)):
if i == 0:
print(names[i] + ':', end='')
if i > 0:
print(names[i])
find_their_numbers('test1.txt')
You can do that without finding individually each number:
def find_their_numbers(text_file):
with open(text_file) as f:
lines = f.read().splitlines()
for line in lines:
line_split = line.split(',')
name = line_split[0]
total = sum([int(x) for x in line_split[1:]])
print(name + ": " + str(total))
Sample test:
>>> find_their_numbers('test1.txt')
rshall Rogers: 199
Richard Lao: 30
Peter Jones: 155
AABB CC EE RR rest: 100
Try this:
file = open(files)
lines = file.read().splitlines()
for name in lines:
names = name.split(',')
print(f"{names[0]}: {sum(map(int,names[1:]))}")
where sum(map(int,names[1:])) will slice names from the second element, convert all the elements to integers and sum them.
You can use unpacking to separate the name from the rest of the components:
with open(fileName,'r') as file:
for line in file.readLines():
name,*numbers = line.split(',')
print(name + ":", sum(map(int,numbers)))
Related
The text file looks like
data/File_10265.data:
Apple:2kg
Apple:3kg
Banana:1kg
Banana:4kg
Some string1
data/File_10276.data:
Apple:6kg
Apple:5kg
Apple:3kg
Banana:2kg
Banana:4kg
Banana:2kg
Banana:4kg
Extra line
data/File_10278.data:
Apple:3kg
Banana:2kg
Banana:4kg
Banana:2kg
Banana:7kg
Some words
The code is as follows:
import re
import pandas as pd
f = open("Samplefruit.txt", "r")
lines = f.readlines()
Apple_count=0
Banana_count=0
File_count=0
Filename_list=[]
Apple_list=[]
Banana_list=[]
for line in lines:
match1=re.findall('data/(?P<File>[^\/]+(?=\..*data))',line)
if match1:
Filename_list.append(match1[0])
print('Match found:',match1)
if line.startswith("Apple"):
Apple_count+=1
elif line.startswith("Banana"):
Banana_count+=1
Apple_list.append(Apple_count)
Banana_list.append(Banana_count)
df=pd.DataFrame({'Filename': Filename_list,'Apple':
Apple_list,'Banana':
Banana_list})
The desired output:
Filename: |Apple |Banana
File_10265|2 |2
File_10276|3 |4
File_10278|1 |4
Maybe there is a more efficient way to do this but here's one solution:
with open('filetest.txt') as f:
lines = f.readlines()
unique_lines = list(dict.fromkeys(lines))
for line in unique_lines:
print(line + str(lines.count(line)))
f1 = open('file.txt', 'a')
f1.write(line + str(lines.count(line)))
f1.close()
You simply open the file, read all lines into a list, then get rid of any duplicates. Then you loop through the list (now with the duplicates removed), and use the .count (docs) function to get the number of occurrences of each unique item in the list.
Try this,
pattern = re.compile(r"data/File_[\d]+.data:")
lines = text.split("\n")
files = itertools.groupby(lines, lambda line:pattern.search(line) == None)
for k, content in files:
if k == True:
content = list(content)
all_words = list(set(content))
counts = {word:content.count(word) for word in all_words if word != ""}
print(counts)
Output -
{'Banana:': 2, 'Apple:': 2}
{'Banana:': 4, 'Apple:': 3}
{'Banana:': 4, 'Apple:': 1}
NOTE: New changes have been made to the code as per the changes in the question.
Try this:
import re
text = {}
def unit_cal(val1, val2): #function to add quantities with units and return the final answer with units
q1 = re.findall("[0-9]+", val1)
unit = re.findall("[a-zA-Z]+", val1)
if (val2 != False):
q2 = re.findall("[0-9]+", val2)
ans = int(q1[0]) + int(q2[0])
else:
ans = int(q1[0])
return str(ans) + unit[0] #remove + unit[0] to return only the value
with open("item.txt", "r") as f1:
for line in f1:
if ("data" in line):
temp_key = line
k = {}
text[temp_key] = k
elif (line.strip() != ""):
temp_word = line.strip().split(":")
if temp_word[0] in text[temp_key]:
text[temp_key][temp_word[0]] = unit_cal(temp_word[1], text[temp_key][temp_word[0]])
else:
text[temp_key][temp_word[0]] = unit_cal(temp_word[1], False)
final_text = ""
for main_key in text:
final_text += main_key + "\n"
for sub_key in text[main_key]:
final_text += sub_key + " : " + str(text[main_key][sub_key]) + "\n\n"
print(final_text) #final output is displayed in the idle
with open("new_items.txt", "w") as f2:
f2.write(final_text) #the output is also written to a new file
Output:
data/File_10265.data:
Apple : 5kg
Banana : 5kg
data/File_10276.data:
Apple : 14kg
Banana : 12kg
data/File_10278.data:
Apple : 3kg
Banana : 15kg
Here, I have posted an answer. Thanks, #Mani,#CarySwoveland, #Zero, and #M B for your support. The code is as follows:
import pandas as pd
text = {}
with open(r"Samplefruit.txt", "r") as file:
for line in file:
if "data" in line:
Filename=line.split('/')[-1].split('.')[0]
Apple_count=0
Banana_count=0
print('----------------')
print(Filename)
elif ("Apple" in line or "Banana" in line):
if line.startswith("Apple"):
Apple_count+=1
elif line.startswith("Banana"):
Banana_count+=1
print('Apple:',Apple_count)
print('Banana:',Banana_count)
text[Filename] = {'Apple':Apple_count,'Banana':Banana_count}
File_list.append(Filename)
df = pd.DataFrame(
{"Filename": text.keys(), "Apple": [x['Apple'] for x in text.values()],"Banana": [x['Banana'] for x in text.values()]}
)
print(df)
Here is my code:
inputFile = open("Employees.txt", "r").read()
inputList = inputFile.split("\n")
fList = []
def listString(s):
string = ""
return (string.join(s))
for i in inputList:
for x in i.split(","):
fList.append(x)
for y in range (len(fList)):
**if fList[y] == "90000":
fList[y] = str(90000 * 1.05) + "\n"
elif fList[y] == "75000":
fList[y] = str(75000 * 1.05) + "\n"
elif fList[y] == "110000":
fList[y] = str(110000 * 1.05) + "\n"
else:
fList[y] = fList[y] + ","**
print(listString(fList))
file = open("Emp_Bonus.txt", "a")
file.write(listString(fList))
Employees.txt contains the following:
Adam Lee,Programmer,90000
Morris Heather,DA,75000
John Lee,PM,110000
I am trying to get the following output:
Adam Lee,Programmer,94500
Morris Heather,DA,78750
John Lee,PM,115500
The part of the code that is in bold is the problem, The input salaries need to be able to be different values instead of the code only working for the sample input. The input salaries have to be multiplied by 1.05. How should I go about doing this? Thanks!
Another way without any library. Just read lines of the file as a list using readlines() and then iterate each line. Only modify the last part after splitting it using split(',') e.g salary of each line and finally create the new file as per the requirements.
multiply, final_result = 1.05, []
with open('Employees.txt', 'r') as f:
fList = f.readlines()
if fList:
for line in fList:
employee_info = line.split(',')
name = employee_info[0]
designation = employee_info[2]
salary = float(employee_info[2].replace('\n','').strip()) * multiply
final_result.append(f"{name},{employee_info[1]},{salary}")
if final_result:
with open('Emp_Bonus.txt', 'w') as f:
f.write('\n'.join(final_result))
Output:
Adam Lee,Programmer,94500.0
Morris Heather,DA,78750.0
John Lee,PM,115500.0
I will like to use Pandas:
import pandas as pd
df = pd.read_csv("Employees.txt",header=None)
df[2] = df.loc[df[2].isin([90000,75000,110000]),2]*1.05
df[2] = df[2].astype(int)
df.to_csv("Emp_Bonus.txt",mode="a",header=None)
I've got the .txt file like this within:
Crista
Jame
7,3
2,0
Wiki
Rok
4,1
6,2
3,2
6,8
Pope
Lokk
5,2
0,1
3,1
Sam
Antony
4,3
9,1
My code to find all names and append them to the names[] list, and to find all digits and append them to the digits[] list (if there are more than two lines with digits in a row I didn't need them in the list before):
import re
f=open('mine.txt')
names=[]
digits=[]
count=0
for line in f:
line = line.rstrip()
if re.search('^[a-zA-Z]', line):
name=line
names.append(name)
if re.findall('^\d{1}:\d{1}', line):
if count < 2 :
digit=line
digits.append(digit)
count += 1
elif line != "" :
count = 0
Then I made pairs for matching names and digits:
my_pairs_dig=list()
while(digits):
a = digits.pop(0); b = digits.pop(0)
my_pairs_dig.append((a,b))
my_pairs_dig
my_pairs_names = list()
while(names):
a = names.pop(0); b = names.pop(0)
my_pairs_names.append((a,b))
my_pairs_names
outp=list(zip(my_pairs_names,my_pairs_dig))
And got this output:
[(('Crista', 'Jame'), ('7,3', '2,0')), (('Wiki', 'Rok'), ('4,1', '6,2')), (('Pope', 'Lokk'), ('5,2', '0,1')), (('Sam', 'Antony'),('4,3', '9,1'))]
But plans were changed and now my desired outout is:
[(('Crista', 'Jame'), ('7,3', '2,0')), (('Wiki', 'Rok'), ('4,1', '6,2'), ('3,2', '6,8')), (('Pope', 'Lokk'), ('5,2', '0,1'), ('3,1')), (('Sam', 'Antony'),('4,3', '9,1'))]
How can I rewrite my code to got the desired outoput?
Try this
with open('test.txt', 'r') as fp:
data = fp.read().split("\n")
i, res = 0, []
while i < len(data):
if data[i].isalpha():
names = (data[i], data[i+1])
i += 2
digits = []
while i < len(data) and not data[i].isalpha():
digits.append(data[i])
i += 1
digits = tuple(digits)
if len(digits) > 2:
res.append((names, digits[: 2], digits[2: ]))
else:
res.append((names, digits[: 2]))
print(res)
Output:
[(('Crista', 'Jame'), ('7,3', '2,0')), (('Wiki', 'Rok'), ('4,1', '6,2'), ('3,2', '6,8')), (('Pope', 'Lokk'), ('5,2', '0,1'), ('3,1',)), (('Sam', 'Antony'), ('4,3', '9,1'))]
Try this:
import re
digits=[]
result = []
name1, name2 = None, None
for line in f:
if line:
line = line.rstrip()
if re.search('^[a-zA-Z]', line):
if name1 and name2:
result.append(((name1, name2), *tuple(tuple(digits[i:i+2]) for i in range(0, len(digits), 2))))
name1, name2, digits = None, None, []
if name1:
name2 = line
else:
name1 = line
else:
digits.append(line)
if name1 and name2:
result.append(((name1, name2), *tuple(tuple(digits[i:i+2]) for i in range(0, len(digits), 2))))
name1, name2, digits = None, None, []
print(result)
Output:
[(('Crista', 'Jame'), ('7,3', '2,0')), (('Wiki', 'Rok'), ('4,1', '6,2'), ('3,2', '6,8')), (('Pope', 'Lokk'), ('5,2', '0,1'), ('3,1',)), (('Sam', 'Antony'), ('4,3', '9,1'))]
This is based on your assumption:
that's always two names and then 2,3 or 4 lines with numbers
So I have this task where I have to count gamers' scores from a text file and then print the scores. How can I split the name and the score and the count the score for specific name? And the names have to be in alphabetical order.
I have tried this:
file = input("Enter the name of the score file: ")
print("Contestant score:")
open_file = open(file, "r")
list = open_file.readlines()
open_file.close()
for row in sorted(list):
row = row.rstrip()
contestant = row.split(" ")
if contestant[0] == contestant[0]:
total = int(contestant[1]) + int(contestant[1])
print(contestant[0], total)
print(row)
example of the text file:
sophia 2
sophia 3
peter 7
matt 10
james 3
peter 5
sophia 5
matt 9
And the program should look like this:
james 3
matt 19
peter 12
sophia 10
And my current output is:
sophia 10
sophia 5
I would suggest making a dictionary:
# Open Text File
file = input("Enter the name of the score file: ")
print("Contestant score:")
open_file = open(file, "r")
lines = open_file.readlines()
# Convert into one single line for ease
mystr = '\t'.join([line.strip() for line in lines])
# Split it and make dictionary
out = mystr.split()
entries = dict([(x, y) for x, y in zip(out[::2], out[1::2])])
# Unsorted Display
print(entries)
# Sorted Display
print(sorted(entries.items(), key=lambda s: s[0]))
Output:
[('james', '3'), ('matt', '9'), ('peter', '5'), ('sophia', '5')]
You can show/save this dictionary in any form you like like CSV, JSONor keep it like this only.
You can use collections.defaultdict for this.
Code:
from collections import defaultdict
file = input("Enter the name of the score file: ")
results = defaultdict(int)
with open(file, "r") as file:
# Gather results from each line.
for line in file:
name, points = line.split()
results[name] += int(points)
# Print the results dict.
print("Contestant score:")
for name in sorted(results.keys()):
print(name, results[name])
This works for Python 3.
Output:
Enter the name of the score file: input.txt
Contestant score:
james 3
matt 19
peter 12
sophia 10
You can use a dictionary to store the total score of each gamer.
from collections import defaultdict
scores=defaultdict(int)
with open('score.txt','r') as f:
for line in f:
if line.strip():
key,val=line.split()
scores[key]+=int(val)
print(*sorted(scores.items()),sep='\n')
Output:
('james', 3)
('matt', 19)
('peter', 12)
('sophia', 10)
You can use something like:
import os
from collections import defaultdict
file = input("Enter the name of the score file: ").strip()
results = defaultdict(int)
if(os.path.isfile(file)):
with open(file) as f:
for row in sorted([x.strip() for x in list(f) if x]):
line = row.split()
if len(line) == 2:
results[line[0]] += int(line[1])
print("Contestants scores:")
for k, v in results.items():
print(k, v)
else:
print("File not found", file)
Contestants scores:
james 3
matt 19
peter 12
sophia 10
demo
I had worked up a solution just using a standard dictionary (not collections.defaultdict), so figured I'd post it too.
Python 3
Code:
file = input("Enter the name of the score file: ")
print("Contestant score:")
gamer_dict = {}
with open(file, 'r') as infile:
for row in infile:
splits = row.split(" ")
name = splits[0].strip()
score = int(splits[1].strip())
if name not in gamer_dict:
gamer_dict[name] = score
else:
gamer_dict[name] += score
for k,v in sorted(gamer_dict.items()):
print(k,v)
Output:
Enter the name of the score file: gamers.txt
Contestant score:
james 3
matt 19
peter 12
sophia 10
File Name: records.csv
Sample file contents:
11, Owen, 17
4, Hank, 18
77, Paul, 10
8, Ryan, 35
12, Patrick, 24
def getFileName():
fileName = input('Input File Name: ')
return fileName
def processFile(fileName):
file = open(fileName, 'r')
lines = file.readlines()
fileList = []
info = []
pts = []
for a in lines:
fileList = a.split(',')
fileList[-1] = int(fileList[-1])
info.append(fileList)
print('-----------------------------------------------')
print('##\t Player\t Points')
print('-----------------------------------------------')
for a in info:
print(a[0],'.','\t', a[1],'\t\t', a[-1], sep='')
print('-----------------------------------------------')
for a in info:
pts.append(a[-1])
maxPts = max(pts)
# Find maxPts in one of the 5 sublist, and make a brand new list of it.
print('Top Scorer:', 'Points:', maxPts)
file.close()
def main():
fileName = getFileName()
processFile(fileName)
main()
Like mentioned in the note above, the list, 'info', is made up of sublists with each containing a line from the 'records.csv' text file. So the first sublist, for example is ['11', ' Owen', 17]. I already found the max 'points' from all the sublists, 35 in this case, and would like to be able to identify the sublist that contains that element, and then print elements from said sublist. Any help is appreciated, thank you.
Have updated your code to do what is required
def getFileName():
fileName = input('Input File Name: ')
return fileName
def processFile(fileName):
file = open(fileName, 'r')
lines = file.readlines()
fileList = []
info = []
pts = []
for a in lines:
fileList = a.split(',')
fileList[-1] = int(fileList[-1])
info.append(fileList)
print('-----------------------------------------------')
print('##\t Player\t Points')
print('-----------------------------------------------')
for a in info:
print(a[0],'.','\t', a[1],'\t\t', a[-1], sep='')
print('-----------------------------------------------')
for a in info:
pts.append(a[-1])
maxPts = max(pts)
index=pts.index(maxPts) #this finds index of the element with higest score
a=info[index]
print(a[0],'.','\t', a[1],'\t\t', a[-1], sep='') #prints the list with higest score
print
# Find maxPts in one of the 5 sublist, and make a brand new list of it.
print('Top Scorer:', 'Points:', maxPts)
file.close()
def main():
fileName = getFileName()
processFile(fileName)
main()
If you only need a min or max value, it is cheapest to sort the list and then get the value that you need.
info = [
[0, "a", 121],
[1, "aa", 14],
[2, "b", 55]
]
print(sorted(info, key=lambda x:x[2])[0])
print(sorted(info, key=lambda x:x[2])[-1])
I am writing the logic, you can put it into function:
f=open('records.csv','r')
filecontent = f.readlines()
lists = [line.strip('\n').split(',') for line in filecontent if line.strip('\n')]
maxpts_sublst = max(lists,key = lambda x:int(x[-1]))
print("Top scorer: {0}, Points {1}".format(*maxpts_sublst[1:]))
f.close()
Output:
Top scorer: Ryan, Points 35