Path problem for Mac version of Python open file

Source: Internet
Author: User

Original address: https://www.jianshu.com/p/b8110d578155

1. File path selection
Mac does not exist in the address bar, sometimes you go to get the file path pasted to open will error
File b‘***.csv‘ does not exist
And you don't want to find the path of the data file every time, the simplest way is to get the path of your edited Ipython notebook, and then put the data file under that path, and then enter the file name directly to open the files.

#获取默认路径import Osprint (Os.path.abspath ('. '))

For example, my path is
/Volumes/LXQ/inotebooks/data
Next, drag the data file, such as Rawdata.csv, into the Files folder, and then open the
data_file = pd.read_csv(‘rawdata.csv‘)
It's OK, you don't have to enter the path.

2, coding problems
When prompted
‘utf-8‘ codec can‘t decode byte 0xd0 in position 0: invalid continuation byte
, it means that the coding is wrong.
This problem will also be encountered when crawling Web pages. The reason is that Python encoding is not the same as crawling down the page code, Python can not read, only return garbled.
The solution is:
First figure out the python default code

#获取系统默认编码:import sysprint(sys.getdefaultencoding())

Return
utf-8
OK if the page you crawl is encoded as utf-8, the result of crawling down will be displayed normally. But it is another matter to save the crawled data to a CSV file, so that Python reads it again, because the CSV document does not have to be utf-8 encoded, and it needs decoding

import numpy as npdata_file=pd.read_csv(‘rawdata.csv‘,encoding=‘gb2312‘)print (data_file[0:3])

The content behind the encoding depends on the encoding of the CSV file.

Path problem for Mac version of Python open file

Related Article

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.