第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

將一行數(shù)據(jù)附加到大型 CSV 文件中的第 2 行

將一行數(shù)據(jù)附加到大型 CSV 文件中的第 2 行

我確信這是一個(gè)非常簡(jiǎn)單的問(wèn)題,但我似乎找不到任何相關(guān)信息。我有一個(gè)非常大的 CSV 文件,我需要在標(biāo)題后面直接插入一行,這有助于讀取 csv 并將其連接到地塊 shapefile 的另一個(gè)代碼。我有代碼來(lái)附加我想要的數(shù)據(jù)行,但它只會(huì)轉(zhuǎn)到最后一行。我無(wú)法弄清楚如何讓代碼在標(biāo)題行之后立即插入我的行。這是我的代碼:import osimport csvinsert_row = '"AAAAAAAAAAAAAAAAAAA","**********","**********","**********","**********","**********","**********","**","**********","**********","****","**********",999999,9999,00'os.chdir(r"D:\PROPERTY\PINELLAS\Data_20201001_t")with open("owner_mail.csv", 'r') as csv_file, open("owner_mail.csv", 'a', newline = "") as new_file:    csv_reader = csv.reader(csv_file)    csv_writer = csv.writer(new_file)    csv_writer.writerow(insert_row)就是這樣了。我只需要 insert_row 行數(shù)據(jù)位于第 2 行位置,而不是位于文件末尾。謝謝。
查看完整描述

2 回答

?
慕尼黑8549860

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超11個(gè)贊

除非替換完全相同長(zhǎng)度的數(shù)據(jù),否則不能只在文件中間插入一行。您必須讀取整個(gè)文件,對(duì)其進(jìn)行編輯,然后重新寫入。


像這樣的東西應(yīng)該有效:


import csv


# This must be an iterable not a string

insert_row = "AAAAAAAAAAAAAAAAAAA","**********","**********","**********","**********","**********","**********","**","**********","**********","****","**********",999999,9999,00


with open("owner_mail.csv", 'r') as csv_file, open("owner_mail_updated.csv", 'w', newline = "") as new_file:

    csv_reader = csv.reader(csv_file)

    csv_writer = csv.writer(new_file)


    header = next(csv_reader)

    csv_writer.writerow(header)


    csv_writer.writerow(insert_row)


    for line in csv_reader:

        csv_writer.writerow(line)

如果 CSV 文件不太大而無(wú)法完全放入內(nèi)存,那么您可以一次讀取所有行,對(duì)其進(jìn)行編輯,然后將它們寫回到同一個(gè)文件中。一旦出現(xiàn)問(wèn)題,風(fēng)險(xiǎn)就更大。更安全地寫入新文件,然后刪除原始文件并重命名(如果沒(méi)有錯(cuò)誤):


import csv


# This must be an iterable not a string

insert_row = "AAAAAAAAAAAAAAAAAAA","**********","**********","**********","**********","**********","**********","**","**********","**********","****","**********",999999,9999,00


with open("owner_mail.csv", 'r') as csv_file:

    rows = list(csv.reader(csv_file))


rows.insert(1,insert_row)  # insert after header row


with open("owner_mail.csv", 'w') as csv_file:

    w = csv.writer(csv_file)

    w.writerows(rows)


查看完整回答
反對(duì) 回復(fù) 2023-09-19
?
素胚勾勒不出你

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個(gè)贊

請(qǐng)嘗試這個(gè):


import os

import csv


insert_row = '"AAAAAAAAAAAAAAAAAA","**********","**********","**********","**********","**********","**********","**","**********","**********","****","**********",999999,9999,00'


with open("owner_mail.csv", 'r') as csv_file, open("owner_mail.csv", 'w') as new_file:

  csv_reader = csv.reader(csv_file)

  reader = list(csv_reader)

  reader.insert(1,insert_row)

  csv_writer = csv.writer(new_file)

  csv_writer.writerows(reader)


查看完整回答
反對(duì) 回復(fù) 2023-09-19
  • 2 回答
  • 0 關(guān)注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)