乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Python使用csv模塊讀寫csv文件

       LibraryPKU 2021-03-12

      csv是逗號分隔值(Comma-Separated Values)的簡稱。

      有時也稱為字符分隔值,因為分隔字符也可以不是逗號,其文件以純文本形式存儲表格數(shù)據(jù)。純文本意味著該文件是一個字符序列,不含必須像二進制數(shù)字那樣被解讀的數(shù)據(jù)。

      csv可以存儲各種各樣的數(shù)據(jù),不過,通常來說,比較適合存儲有相同字段或表頭的一批數(shù)據(jù),這些數(shù)據(jù)可以展示成表格。

      可以使用excel開啟csv文件,打開后看到的數(shù)據(jù)以excel表格的方式進行展示。

      現(xiàn)在我們就開始使用csv將數(shù)據(jù)寫入csv文件,然后將數(shù)據(jù)從csv中讀取出來使用。

      一、將數(shù)據(jù)寫入csv文件中

      1. import csv


      2. csv_data = (
      3. (1, 2, 3, 4, 5, 6),
      4. ('a', 'b', 'c', 'd', 'e', 'f'),
      5. ('p', 'y', 't', 'h', 'o', 'n')
      6. )
      7. output_file_name = 'csv_file.csv'


      8. def save_csv(target_list, output_file_name):
      9. """
      10. 將數(shù)據(jù)寫入csv文件
      11. """
      12. if not output_file_name.endswith('.csv'):
      13. output_file_name += '.csv'
      14. csv_file = open(output_file_name, "w", newline="")
      15. key_data = target_list[0]
      16. value_data = [target for target in target_list]
      17. csv_writer = csv.writer(csv_file)
      18. csv_writer.writerow(key_data)
      19. csv_writer.writerows(value_data)
      20. csv_file.close()


      21. save_csv(csv_data, output_file_name)

      代碼描述:

      1.我們先將需要保存的數(shù)據(jù)解析好,保存成固定的數(shù)據(jù)類型(保存成列表,元組,字典都可以,根據(jù)具體場景來選擇)

      2.我們將保存數(shù)據(jù)到csv文件的代碼封裝成一個函數(shù),方便重用。步驟主要分為三步:打開文件,寫入數(shù)據(jù),關閉文件。其中,寫入數(shù)據(jù)時記得先寫入表頭(我們使用excel打開時需要表頭)再寫入表格中的數(shù)據(jù),數(shù)據(jù)要以一個列表的形式傳遞給writerows()。

      運行結果:

      運行以上代碼后,會在當前目錄下創(chuàng)建一個csv_file.csv的文件,并寫入csv_data的數(shù)據(jù),可以使用excel打開文件查看。如下圖。

      二、從csv文件中讀取數(shù)據(jù)

      1. input_file_name = 'csv_file.csv'


      2. def read_csv(input_file_name):
      3. """
      4. 讀取csv文件數(shù)據(jù)
      5. """
      6. with open(input_file_name, 'r', encoding='utf-8') as csv_file:
      7. csv_reader = csv.reader(csv_file)
      8. # csv_reader對象,是一個列表的格式
      9. print(csv_reader)
      10. # csv_reader對象的一個迭代器,可以通過next()取出其中的元素
      11. print(next(csv_reader))
      12. # 也可以通過for循環(huán)取出所有元素
      13. for line in csv_reader:
      14. print(''.join(line))


      15. read_csv(output_file_name)

      代碼描述:

      1.可以通過with上下文管理的方式打開csv文件,如果想在with的代碼塊外對讀出來的數(shù)據(jù)進行處理,則可以使用open()打開,再使用close()關閉。

      2.csv通過csv.reader()來打開csv文件,返回的是一個列表格式的迭代器,可以通過next()方法獲取其中的元素,也可以使用for循環(huán)依次取出所有元素。

      運行結果:

      1. <_csv.reader object at 0x00000295BC044528>
      2. ['1', '2', '3', '4', '5', '6']
      3. 123456
      4. abcdef
      5. python

      這樣,將數(shù)據(jù)寫入csv和從csv中讀取數(shù)據(jù)就完成了,使用過程是非常簡單的。

       

       

        本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多