Jump to content

warrenfelsh

Member
  • Posts

    2
  • Joined

  • Last visited

Reputation

0 Neutral

Personal Information

  • Location
    United Arab Emirates
  1. In the case you are working with Big Data using readlines() is not very efficient as it can result in MemoryError because this function loads the entire file into memory, then iterates over it. A slightly better approach for large files is to use the fileinput module , as follows: import fileinput for line in fileinput.input(['sample.txt']): print(line) The fileinput.input() call reads lines sequentially, but doesn't keep them in memory after they've been read or even simply so this, since file in Python is iterable.
  2. ast.literal_eval(node_or_string) You can convert a Python string to an int by using ast.literal_eval() . This can be used for safely evaluating strings containing Python values from untrusted sources without the need to parse the values oneself. It is not capable of evaluating arbitrarily complex expressions , for example involving operators or indexing. import ast ast.literal_eval("111") return 111
×
×
  • Create New...