How to add a header to an existing csv file?

A.J.

I know this is a very basic question. I have a CSV file, which contains data already. This file is generated automatically not using opening with Dictreader or open object.

Goal

  1. I want to open an existing file
  2. Append the Header in the first row (Shift the first row data)
  3. Save the file
  4. Return the file

Any clues?

cursor.execute(sql, params + (csv_path,))

This command generates file, without header.

Code

  sql, params = queryset.query.sql_with_params()
    sql += ''' INTO OUTFILE %s
           FIELDS TERMINATED BY ','
           OPTIONALLY ENCLOSED BY '"'
           LINES TERMINATED BY '\n' '''
    csv_path = os.path.join(settings.MEDIA_ROOT + '\\tmp', csv_filename)
cursor = connection.cursor()

cursor.execute(sql, params + (csv_path,))
columns = [column[0] for column in cursor.description] #error

Tried

SELECT `website` UNION SELECT `request_system_potentialcustomers`.`website` FROM `request_system_potentialcustomers` ORDER BY `request_system_potentialcustomers`.`revenue` DESC  
INTO OUTFILE "D:\\out.csv"
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n';
unutbu

Wait a minute. If you have not yet called

cursor.execute(sql, params + (csv_path,))

then you have the opportunity to write the CSV file correctly from the get-go. You should not need to write a new file with the header line, then copy all that CSV into the new file and so forth. That is slow and inefficient -- and your only choice -- if you really have to prepend a line to an existing file.

If instead you have not yet written the CSV file, and if you know the header, then you can add it to the SQL using SELECT ... UNION ... SELECT:

header = ['foo', 'bar', 'baz', ]
query = ['SELECT {} UNION'.format(','.join([repr(h) for h in header]))]
sql, params = queryset.query.sql_with_params()
query.append(sql)
sql = '''INTO OUTFILE %s
         FIELDS TERMINATED BY ','
         OPTIONALLY ENCLOSED BY '"'
         LINES TERMINATED BY '\n' '''
query.append(sql)
sql = ' '.join(query)         
csv_path = os.path.join(settings.MEDIA_ROOT + '\\tmp', csv_filename)

cursor = connection.cursor()
cursor.execute(sql, params + (csv_path,))

Demo:

mysql> SELECT "foo", "bar" UNION SELECT "baz", "quux" INTO OUTFILE "/tmp/out";  

Produces the file /tmp/out containing

foo bar
baz quux

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to add new column(header) to a csv file from command line arguments

분류에서Dev

how to add a header to a file visual basic

분류에서Dev

How to add a xml file header with vb.net

분류에서Dev

How to add JLabel to existing JTextField

분류에서Dev

How to add ngClick to an existing element

분류에서Dev

Add (prepend) line to file, if parameter is not already existing

분류에서Dev

Preserve existing tags in Anki CSV import/batch-add tags

분류에서Dev

How to add an additional Sound track to an existing Video

분류에서Dev

how to add existing project in git using bluemix

분류에서Dev

How to add method to existing object in Coffeescript?

분류에서Dev

How to add a header to a listView which is inside SwipeRefreshLayout?

분류에서Dev

How to add a header and/or footer to a sed or awk stream?

분류에서Dev

jasper reports studio - how to add header information

분류에서Dev

Merging two csv file based on header values in python

분류에서Dev

Calling data from a csv file with its header Python

분류에서Dev

Add data to rows in csv file python

분류에서Dev

How can I view file header information?

분류에서Dev

How to parse of a simple csv file?

분류에서Dev

How to properly add new hard drivers to existing Ubuntu system?

분류에서Dev

How to add new grid rows without clearing existing data

분류에서Dev

How to add a fine in existing clearcase baseline? (Not the latest baseline)

분류에서Dev

How to add default constraint on an existing column in SQL Server

분류에서Dev

How to not read the header row while reading csv using Scanner?

분류에서Dev

How to mass add file extension?

분류에서Dev

How to add elements in an XML file?

분류에서Dev

How to add new line in a file

분류에서Dev

How to add custom attribute to customer export csv

분류에서Dev

How to add checkbox column with checkbox header in gridview dynamically?

분류에서Dev

Add existing user to Unity

Related 관련 기사

  1. 1

    How to add new column(header) to a csv file from command line arguments

  2. 2

    how to add a header to a file visual basic

  3. 3

    How to add a xml file header with vb.net

  4. 4

    How to add JLabel to existing JTextField

  5. 5

    How to add ngClick to an existing element

  6. 6

    Add (prepend) line to file, if parameter is not already existing

  7. 7

    Preserve existing tags in Anki CSV import/batch-add tags

  8. 8

    How to add an additional Sound track to an existing Video

  9. 9

    how to add existing project in git using bluemix

  10. 10

    How to add method to existing object in Coffeescript?

  11. 11

    How to add a header to a listView which is inside SwipeRefreshLayout?

  12. 12

    How to add a header and/or footer to a sed or awk stream?

  13. 13

    jasper reports studio - how to add header information

  14. 14

    Merging two csv file based on header values in python

  15. 15

    Calling data from a csv file with its header Python

  16. 16

    Add data to rows in csv file python

  17. 17

    How can I view file header information?

  18. 18

    How to parse of a simple csv file?

  19. 19

    How to properly add new hard drivers to existing Ubuntu system?

  20. 20

    How to add new grid rows without clearing existing data

  21. 21

    How to add a fine in existing clearcase baseline? (Not the latest baseline)

  22. 22

    How to add default constraint on an existing column in SQL Server

  23. 23

    How to not read the header row while reading csv using Scanner?

  24. 24

    How to mass add file extension?

  25. 25

    How to add elements in an XML file?

  26. 26

    How to add new line in a file

  27. 27

    How to add custom attribute to customer export csv

  28. 28

    How to add checkbox column with checkbox header in gridview dynamically?

  29. 29

    Add existing user to Unity

뜨겁다태그

보관