Text files are basic file types, whether CSV, XLS, JSON, XML, and so on, can be read as text files.
#-*-coding:utf-8-*-Fpath ="Data/textfile.txt"F= Open (Fpath,'R')## Read characters by characterFirst_char = F.read (1)Print "First Char:", First_char## Change the location of the file object, the location is calculated according to ByteSize## If you don't move the position to the beginning, then the reading starts at the current position.f.seek (0)## Read all the contents of the fileAll_chars =F.read ()PrintAll_chars## Read data by linef.seek (0) First_line=F.readline ()Print 'First line :', First_line## Read All rowsf.seek (0) All_lines=f.readlines () num_lines=Len (all_lines)Print "Number of lines:", Num_lines forI, lineinchF:PrintI, line
In Data science, JSON is also one of the most commonly used file forms, and Python has a good support for JSON.
Import= open ('data/donut.json'r') # # According to JSON, the data read is either list or Dict- json.load (f)print" " , type (data) Print Data
[Data Science] extract data from text, JSON files