简体   繁体   English

将一行数据附加到大型 CSV 文件中的第 2 行

[英]Appending a row of data to line 2 in a large CSV File

I'm sure this is a really easy question but I can't seem to find any information on it.我确信这是一个非常简单的问题,但我似乎找不到任何相关信息。

I have a very large CSV file which I need to insert a row directly after the header which helps with another code that reads the csv and joins it to a parcel shapefile.我有一个非常大的 CSV 文件,我需要在标题后直接插入一行,这有助于另一个读取 csv 并将其连接到包裹 shapefile 的代码。

I have the code to append the row of data that I want, but it will only go to the last line.我有代码来附加我想要的数据行,但它只会转到最后一行。 I cannot figure out how to get the code to insert my row immediately after the header row.我不知道如何让代码在标题行之后立即插入我的行。 Here is my code:这是我的代码:

import os
import csv

insert_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)

So that's it.就是这样了。 I just need the insert_row line of data to be in row position number 2 instead of at the end of the file.我只需要 insert_row 数据行位于第 2 行位置而不是文件末尾。 Thank you.谢谢你。

You can't just insert a row in the middle of a file unless replacing data of exactly the same length.除非替换长度完全相同的数据,否则您不能只在文件中间插入一行。 You have to read the entire file, edit it, and re-write it.您必须阅读整个文件,对其进行编辑并重新编写。

Something like this should work:这样的事情应该工作:

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)

If the CSV file is not too large to fit entirely in memory than you can read all the lines at once, edit them, and write them back out to the same file.如果 CSV 文件不是太大而无法完全容纳在内存中,那么您可以一次读取所有行,编辑它们,然后将它们写回到同一个文件中。 It's riskier if there is a problem.如果出现问题,风险更大。 Safer to write to a new file, then delete original and rename if no errors:写入新文件更安全,如果没有错误,则删除原始文件并重命名:

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)

Please try this:请试试这个:

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)


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM