[Data Science] extract data from text, JSON files

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.