Computer Science
Write a function begEnd() in Python to read lines from text file 'TESTFILE.TXT' and display the first and the last character of every line of the file (ignoring the leading and trailing white space characters).
Example: If the file content is as follows:
An apple a day keeps the doctor away.
We all pray for everyone's safety
A marked difference will come in our country.
Then begEnd () function should display the output as: A. Wy A.
Python
Python File Handling
3 Likes
Answer
def begEnd():
file = open('TESTFILE.TXT', 'r')
f = file.readlines()
for line in f:
line = line.strip()
if line:
first_char = line[0]
last_char = line[-1]
print(first_char + last_char, end=' ')
begEnd()Output
A. Wy A.
Answered By
2 Likes
Related Questions
Write a function WordsList(S), where S is a string. The function returns a list, named Words, that stores all the words from the string which contain 'r'.
For example, if S is "Dew drops were shining in the morning", then the list Words should be: ['drops', 'were', 'morning']
In a database BANK, there are two tables with a sample data given below:
Table: EMPLOYEE
ENo EName Salary Zone Age Grade Dept 1 Mona 70000 East 40 A 10 2 Mukhtar 71000 West 45 B 20 3 Nalini 60000 East 26 A 10 4 Sanaj 65000 South 36 A 20 5 Surya 58000 North 30 B 30 Table: DEPARTMENT
Dept DName HOD 10 Computers 1 20 Economics 2 30 English 5 (a) To display ENo, EName, Salary and corresponding DName of all the employees whose age is between 25 and 35 (both values inclusive).
(b) To display DName and corresponding EName from the tables DEPARTMENT and EMPLOYEE (Hint: HOD of DEPARTMENT table should be matched with ENo of EMPLOYEE table for getting the desired result).
(c) To display EName, Salary, Zone and Income Tax (Note: Income tax to be calculated as 30% of salary) of all the employees with appropriate column headings.
Write a function reverseFile() in Python to read lines from text file 'TESTFILE.TXT' and display the file content in reverse order so that the last line is displayed first and the first line is displayed at the end.
Consider the following table STUDENT.
NO NAME AGE DEPARTMENT FEE SEX 1 PANKAJ 24 COMPUTER 120 M 2 SHALINI 21 HISTORY 200 F 3 SANJAY 22 HINDI 300 M 4 SUDHA 25 HISTORY 400 F Write a Python code to search a record as per given NO (number) using MySQL connectivity and print the data.