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