Miscellaneous

Answer:

You can open a file in python by ‘open’ function. open() returns a file object, and is most commonly used with two arguments: open(filename, mode).

f = open('workfile.txt', 'r')

f is file handle here.
We should always close a file handle after doing stuffs.

f.close()

If you want python to close your file, then you could use with

with open("workfile.txt") as f:
    for line in f:
        print(line)

After printing the lines the file will be closed.
If mode field is empty, it will open the file in ‘r’ read mode.

Discuss It

Answer:

To read a file’s contents, call f.read(size), which reads some quantity of data and returns it as a string (in text mode) or bytes object (in binary mode). Size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned.

“somefile.txt” contains following lines
#This is first line
#This is second line
#This is third line

with open("somefile.txt", "r") as f:
  data = f.read()
  print(data)

#Out put
This is first line
This is second line
This is third line

f.readline() reads a single line from the file.

with open("some.txt", "r") as f:
  data = f.readline()
  print(data)

#Out put
This is first line

The readlines() method to get a list of string values from the file, one string for each line of text.

with open("somefile.txt", "r") as f:
  data = f.readlines()
  print(data)


#Output
['This is first line\n', 'This is second line\n', 'This is third line\n']
Discuss It

Answer:

def fib(n):                       # return Fibonacci series up to n
    result = []
    a, b = 0, 1
    while a < n:
        result.append(a)
        a, b = b, a+b
    return result

fib(100)                          # [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Discuss It

Answer:

f.write(string) writes the contents of string to the file and returns the number of characters written.

f = open('workfile.txt', 'w')
Print(f.write('This is a test\n'))
15
Discuss It

Answer:

The Regex method search() returns a match object of the first matched text in the searched string. By calling the group method on match object we can get the matched string. If multiple groups are there we can get them by passing integer 1,2 ..etc to the group.

my_regex = re.compile('(a)(b)')
mo = my_regex.search('cab')       # mo is matching object
print(mo.group())
'ab'
print(mo.group(1))
'a'

The method match() is similar to search() but it only matches at the beginning of string.

my_regex = re.compile('(a)(b)')
mo = my_regex.match('cab')
print(mo.group())
None
mo = my_regex.match('abc')
print(mo.group())
'ab'
print(mo.group(1))
'a'
Discuss It

Answer:

The expression re.findall() returns list of strings of every match in the searched string. If there are groups in the regular expression, then findall() will return a list of tuples.

import re
print(re.findall(r'\d','9 is less than 15'))
['9', '1', '5']

The expression re.finditer() returns an iterator yielding match object instances over all non-overlapping matches for the re pattern in the string.

print(list(map(lambda x: x.group(),re.finditer(r'\d','9 is less than 15'))))
['9', '1', '5']
Discuss It

Quiz

on
Miscellaneous

Tutorial

on
Miscellaneous

Programs

on
Miscellaneous
Click any Link
to navigate to certain page easily
Write a line to us
Your Email
Title
Description