Python3 using CSV module to read and write CSV files

Source: Internet
Author: User

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.cutThe 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_searchThe 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.cutAnd 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.lcutand 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.dtas 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

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.