Formatting strings from a text file - python

I'm trying to format my text line by line read from a .txt file. Firstly I'm just trying to figure out how to print just one line, then attempt multiple lines after. I've managed to print the text of one line, but when I try to format it they way I want it to look, a new line is created after I attempt to print the last word of the line (using index of -1). The last word is creating a new line, so I think I need to find a way to read the lines as seperate strings but I'm not sure.
This is the code for my program:
def makeTuple (employee):
myTuple = employee.split(" ")
payroll, salary, job_title, *othernames, surname = myTuple
myTuple = tuple(myTuple)
return(myTuple)
def printTuple (data):
employee_str = "{}, {} {:>8} {} {:>5}"
print(employee_str.format(data[-1], " ".join(data[3:-1], data[0], data[2], data[1]))
get_file = input(str("Please enter a filename: "))
path = get_file + ".txt"
try:
text_file = open(path, "r")
except IOError:
print('The file could not be opened.')
exit()
record = text_file.readline()
myTuple = makeTuple(record)
printTuple(myTuple)
This is the text file I'm reading from:
12345 55000 Consultant Bart Simpson
12346 25000 Teacher Ned Flanders
12347 20000 Secretary Lisa Simpson
12348 20000 Wizard Hermione Grainger
The output I get at the moment is:
Simpson
, Bart 12345 Consultant 55000
Whereas I want it to look like:
Simpson, Bart 12345 Consultant 55000

You are getting a newline after "Simpson" because that's what's in the text file.
Use the function strip() (Documentation) to remove the newlines when you read the line.
Change the following one line of code and your program should work.
record = text_file.readline().strip()

You can do it with split and join:
s = '''Simpson
, Bart 12345 Consultant 55000'''
print(''.join(s.split('\n')))

Related

Not use for loop to read the file

A file, grades.dat, that includes the names and grades of 10
students as shown below are given as an example.The first item is the student's name and the second item is the grade of that student. The code will read the information in the file in a dictionary where student names are keys and their grades are values. The second item in each line that represents a grade will be converted to float
when it is read as a value in the dictionary. The code is expected to print the names
and grades of the students after reading them. A grade is considered success if it
is 70 or above, and failure if it is below 70.
Grades.dat
Barbara 62.5
John 85.
Marvin 72.5
Lewis 95.
William 65.
Mary 87.5
Sandra 92.5
Jacob 60.
Leigh 75.
Pamela 67.5
file = input("Enter the file name: ")
try:
f=open(file,"r") #c=f.read()
#print(c)
except IOError:
print("file does not exist")
#print(c)
Although not a typical way of doing things... This doesn't use a For Loop:
f=open(file,"r")
while(True):
line = f.readline()
if line == '':
break
# Do stuff to make each line fit the requirements
I'm assuming you cannot use any type of loops. Just for fun, here is another solution to iterate over a list and convert it to dictionary using a list iterator and zip function:
file = input("Enter the file name: ") try:
f=open(file,"r")
# iterator to a list of lines
c=iter(f.read().split())
# zip to create name/grade pairs, then convert to dict
grades_dict = dict(zip(c,c))
print(grades_dict)
except IOError:
print("file does not exist")
I'm not sure about the remaining problem definition. Are you allowed to use a loop for printing the output? This is using list comprehension, which is technically a for loop:
print ('\n'.join(k + ": Success"
if float(v) >= 70.0 else k + ": Failure"
for k,v in grades_dict.items()))
*Edit: It is a "Generator Expression," similar to List Comprehension
https://peps.python.org/pep-0289/

Adding horizontal space between data on a Read only script

I need my output to look nice, and it looks very sloppy.
--------Current output---------
Below are the players and their scores
John Doe 120
Sally Smooth 115
----------End current output----------
My desired output follows
-------Desired output-----------------
Below are the players and their scores
John Doe 120
Sally Smooth 115
--------End desired output-------------
my current code follows;
def main():
# opens the "golf.txt" file created in the Golf Player Input python
# in read-only mode
infile = open('golf.txt', 'r')
print("Below are the players and their scores")
print()
# reads the player array from the file
name = infile.readline()
while name != '':
# reads the score array from the file
score = infile.readline()
# strip newline from field
name = name.rstrip('\n')
score = score.rstrip('\n')
# prints the names and scores
print(name + " " + score)
# read the name field of next record
name = infile.readline()
# closes the file
infile.close()
main()
Try using the tab character to format your spaces better.
print(name + "\t" + score)
This should give you something closer to your desired output. But you may need to use two if some names are long.
You can add the names and the scores to a list and then print it as a table as
import numpy as np
name_list = ['jane doe' ,'sally smooth']
score = np.array([[102,],[106,]]) #make a numpy array
row_format ="{:>15}" * (len(name_list))
for name, row in zip(name_list, score):
print(row_format.format(name, *row))
Note: This depends on str.format()
This code will output:
jane doe 102
sally smooth 106

How can I replace numbered lines with returns in python?

I have a python program that takes a .txt file with a list of information. Then the program proceeds to number every line, then remove all returns. Now I want to add returns to the lines that are numbered without double spacing so I can continue to edit the file. Here is my program.
import sys
from time import sleep
# request for filename
f = raw_input('filename > ')
print ''
# function for opening file load
def open_load(text):
for c in text:
print c,
sys.stdout.flush()
sleep(0.5)
print "Opening file",
open_load('...')
sleep(0.1)
# loading contents into global variable
f_ = open(f)
f__ = f_.read()
# contents are now contained in variable 'f__' (two underscores)
print f__
raw_input("File opened. Press enter to number the lines or CTRL+C to quit. ")
print ''
print "Numbering lines",
open_load('...')
sleep(0.1)
# set below used to add numbers to lines
x = f
infile=open(x, 'r')
lines=infile.readlines()
outtext = ['%d %s' % (i, line) for i, line in enumerate (lines)]
f_o = (str("".join(outtext)))
print f_o
# used to show amount of lines
with open(x) as f:
totallines = sum(1 for _ in f)
print "total lines:", totallines, "\n"
# -- POSSIBLE MAKE LIST OF AMOUNT OF LINES TO USE LATER TO INSERT RETURNS? --
raw_input("Lines numbered. Press enter to remove all returns or CTRL+C to quit. ")
print ''
print "Removing returns",
open_load('...')
sleep(0.1)
# removes all instances of a return
f_nr = f_o.replace("\n", "")
# newest contents are now located in variable f_nr
print f_nr
print ''
raw_input("Returns removed. Press enter to add returns on lines or CTRL+C to quit. ")
print ''
print "Adding returns",
open_load('...')
sleep(0.1)
Here is an example of what I need. In my code, here are no returns (\n) in this below. I have the terminal set to where the lines are in order without having returns (\n).
1 07/07/15 Mcdonalds $20 1 123 12345
2 07/07/15 Mcdonalds $20 1 123 12345
3 07/07/15 Mcdonalds $20 1 123 12345
4 07/07/15 Mcdonalds $20 1 123 12345
5 07/07/15 Mcdonalds $20 1 123 12345
The numbering, 1-5, needs to be replaced with returns so each row is it's own line. This is what it would look like in after being edited
# the numbering has been replaced with returns (no double spacing)
07/07/15 Mcdonalds $20 1 123 12345
07/07/15 Mcdonalds $20 1 123 12345
07/07/15 Mcdonalds $20 1 123 12345
07/07/15 Mcdonalds $20 1 123 12345
07/07/15 Mcdonalds $20 1 123 12345
According to your input/ouptut data sample:
g = open(output_filename, 'w')
f = open(filename)
sep = ' ' # separator character
for line in f:
L = line.split()[1:] # remove the first item - the line number
g.write(sep.join(L)) # rewrite the line without it
g.close()
f.close()
I've realized that this isn't going to solve my issue. - My issue is that there are returns within the original data of the file that are in the wrong place. So I wrote a program to number every line before I took out all of the returns, then I wanted to replace the numbers in front of the lines with new returns in order to make everything correct. Thanks for all you guy's help! I should've seen that before I even started, lol.

Import a file and putting it inside a tuple in a particular format (python)

I am really stuck on this code that i've been working on and for 9 hours straight I cannot get it to work. Basically I am importing a file and splitting it to read the lines one by one and one of the tasks is to rearrange the lines in the file, for example the first line is: 34543, 2, g5000, Joseph James Collindale should look like : ['Collindale, Joseph James', '34543', g5000', '2']. So essentially it should loop over each line in the file and rearrange it to look like that format above. I created a function to check whether the length of the line is either 5 or 6 because they would both have a different format.
def tupleT(myLine):
myLine = myLine.split()
if len(myLine) == "5":
tuple1 = (myLine[4],myLine[3],myLine[0],myLine[2],myLine[1])
return tuple1
elif len(myLine) == "6":
tuple1 = (myLine[5],myLine[3]+ myLine[4],myLine[0],myLine[2], myLine[1])
return tuple1
mylist = []
x = input("Enter filename: ")
try :
f = open(x)
myLine = f.readline()
while (len(myLine)>0):
print(myLine[:-1])
myLine = f.readline()
tupleT(myLine)
f.close()
except IOError as e :
print("Problem opening file")
This is what the original file looks like in textpad and its called studs.txt:
12345 2 G400 Bart Simpson
12346 1 GH46 Lisa Simpson
12347 2 G401 Homer J Simpson
12348 4 H610 Hermione Grainger
12349 3 G400 Harry Potter
12350 1 G402 Herschel Shmoikel Krustofski
13123 3 G612 Wayne Rooney
13124 2 I100 Alex Ferguson
13125 3 GH4P Manuel Pellegrini
13126 2 G400A Mike T Sanderson
13127 1 G401 Amy Pond
13128 2 G402 Matt Smith
13129 2 G400 River Storm
13130 1 H610 Rose Tyler
Here is some commented code to get you started. Your code was a bit hard to read.
Consider renaming first, second and third since I have no idea what they are...
#!/usr/bin/env python
# this is more readable since there are actual names rather than array locations
def order_name(first, second, third, first_name, middle_name, last_name=None):
if not last_name:
# if there is no last name we got the last name in middle name (5 arguments)
last_name = middle_name
else:
# if there is a middle name add it to the first name to format as needed
first_name = "%s %s" % (first_name, middle_name)
return ("%s, %s" % (last_name, first_name), first, third, second)
with open('studs.txt') as o:
# this is a more standard way of iterating file rows
for line in o.readlines():
# strip line of \n
line = line.strip()
print "parsing " + line
# you can unpack a list to function arguments using the star operator.
print "ordered: " + str(order_name(*line.split()))

Python - go to two lines above match

In a text file like this:
First Name last name #
secone name
Address Line 1
Address Line 2
Work Phone:
Home Phone:
Status:
First Name last name #
....same as above...
I need to match string 'Work Phone:' then go two lines up and insert character '|' in the begining of line. so pseudo code would be:
if "Work Phone:" in line:
go up two lines:
write | + line
write rest of the lines.
File is about 10 mb and there are about 1000 paragraphs like this.
Then i need to write it to another file. So desired result would be:
First Name last name #
secone name
|Address Line 1
Address Line 2
Work Phone:
Home Phone:
Status:
thanks for any help.
This solution doesn't read whole file into memory
p=""
q=""
for line in open("file"):
line=line.rstrip()
if "Work Phone" in line:
p="|"+p
if p: print p
p,q=q,line
print p
print q
output
$ python test.py
First Name last name #
secone name
|Address Line 1
Address Line 2
Work Phone:
Home Phone:
Status:
You can use this regex
(.*\n){2}(Work Phone:)
and replace the matches with
|\1\2
You don't even need Python, you can do such a thing in any modern text editor, like Vim.
Something like this?
lines = text.splitlines()
for i, line in enumerate(lines):
if 'Work Phone:' in line:
lines[i-2] = '|' + lines[i-2]

Categories