Python3 using CSV module to read and write CSV files
To read a CSV file:
Import CSV
#打开文件, with the open can not be deliberately closed file, Python3 does not support files () Open, only with open ()
With open ("Xxx.csv", "R", encoding= "Utf-8") as CSVFile:
#读取csv文件, the iteration type is returned
Read = Csv.reader (csvfile)
For I in read:
Print (i)
Save As CSV file:
Import CSV
With open ("Xxx.csv", "W", Newline= "") as Datacsv:
#dialect为打开csv文件的方式, the default is excel,delimiter= "\ T" parameter refers to the delimiter at the time of writing
Csvwriter = Csv.writer (Datacsv,dialect = ("Excel"))
#csv文件插入一行数据, put each item in the following list into a cell (you can insert multiple rows in a loop)
Csvwriter.writerow (["A", "B", "C", "D"])
Description:CSV module also has Dictreader and dictwriter can be used to read and write, return is the type of dictionary, but these two methods I have no use, interested can see for themselves.
Jieba participle
1 participle
jieba.cut
The method accepts three input parameters: A string that requires a word breaker, a cut_all parameter to control whether a full mode is used, and a hmm parameter to control the use of HMM models.
jieba.cut_for_search
The method accepts two parameters: A string that requires a word breaker, or whether to use a HMM model. This method is suitable for the search engine to construct the inverted index word segmentation, the granularity is relatively fine
- The string to be participle can be a Unicode or UTF-8 string, GBK string. Note: It is not recommended to enter the GBK string directly, possibly incorrectly decoded into UTF-8
jieba.cut
And jieba.cut_for_search
The returned structure is an iterative generator, you can use the For loop to get every word (Unicode) you get after a word breaker, or
jieba.lcut
and jieba.lcut_for_search
return directly to list
jieba.Tokenizer(dictionary=DEFAULT_DICT)
Creates a new custom word breaker that can be used to use different dictionaries at the same time. jieba.dt
as the default word breaker, all global word-breaker-related functions are the mappings for this word breaker.
# Encoding=utf-8import jiebaseg_list = Jieba.cut ("I came to Beijing Tsinghua University", cut_all=true) print ("Full Mode:" + "/". Join (seg_list))
# Full Mode seg_list = Jieba.cut ("I came to Beijing Tsinghua University", Cut_all=false) print ("Default mode:" + "/". Join (Seg_list)) # precision Mode seg_list = Jieba.cut ("He came to NetEase Hang Research Building") # Default is the Precision mode print (",". Join (seg_list)) Seg_list = Jieba.cut_for_search ("Xiao Ming graduated from the Chinese Academy of Sciences, After study at Kyoto University in Japan ") # Search engine mode print (", ". Join (Seg_list))
Python3 using CSV module to read and write CSV files