Can't read first row of CSV file

Jack Hillard

I can't figure out why my script is not reading and storing the first row of the CSV file. It is starting with the second row for some reason.

I have the following code (reading from a 2 column CSV file):

Set rs = CreateObject("ADOR.Recordset")

'this just gets the folder where the csv file lives
sDir = GetiMacrosFolder("DataSources")

strConnect = "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
             "DefaultDir=" & sDir & ";"

rs.Open "select * from test.csv", strConnect

count = 0

Do Until rs.EOR
    ReDim Preserve var1(count)
    var1(count) = rs.fields(0)

    ReDim Preserve var2(count)
    var2(count) = rs.fields(1)

    count = count + 1
    rs.MoveNext
Loop

rs.Close

If I then do a MsgBox(var1(1)), it shows me the value in row 3 and not row 2 like it should.

Ansgar Wiechers

The Recordset object reads the first line of the CSV as the table headers, so the second line is the first data row. You can avoid this by using a driver that you can instruct not to do that:

strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sDir & _
             ";Extended Properties=""text;HDR=No;FMT=Delimited"";"

or by placing a schema.ini like this alongside the CSV:

[test.csv]
Format=CSVDelimited
ColNameHeader=False
MaxScanRows=0
CharacterSet=ANSI

Note that CSVDelimited only works when your file is actually comma-separated and you have the comma defined as the field separator character in your system's regional settings. Otherwise you need to specify your delimiter character in that file:

[test.csv]
Format=Delimited(<delimiter>)
ColNameHeader=False
MaxScanRows=0
CharacterSet=ANSI

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

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

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why can't I read whole file?

분류에서Dev

How can I read a big CSV file without Memory exhausted?

분류에서Dev

VB.NET read only some cells for each row from csv file

분류에서Dev

Can't figure out the format this file is expecting to read

분류에서Dev

Android - Java can't read the whole JSON file

분류에서Dev

Can't import special characters from CSV file

분류에서Dev

How can I read from a CSV file into 2 ArrayLists depending on the data type I have got in the File?

분류에서Dev

Save copy of row in csv file

분류에서Dev

table2CSV Skip first row

분류에서Dev

Can't read an Excel file - File.open isn't valid

분류에서Dev

Can't Find File When Using Scanner to read In a Previously Created File

분류에서Dev

Select row to be deleted in csv file in python

분류에서Dev

Importing a CSV file into postgres - skip the first line

분류에서Dev

Read any column from csv file

분류에서Dev

ExecuteScalar doesn't return the first column of the first row

분류에서Dev

C++ File I/O--can't read/write simultaneously?

분류에서Dev

Can't read file from secure_filename(f.filename)

분류에서Dev

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

분류에서Dev

Can python read this type of config file?

분류에서Dev

how can read files in directory and write to file

분류에서Dev

can people read the content of an online php file?

분류에서Dev

Why does the cat command only read from the first file descriptor?

분류에서Dev

cron can't read PYTHONPATH environment variables?

분류에서Dev

Why email can't be read from Maildir?

분류에서Dev

Can't overwrite file on Windows

분류에서Dev

PHPexcel can't save file

분류에서Dev

Can't find the file specified

분류에서Dev

using textscan to read badly formatted CSV file in Octave

분류에서Dev

How to fix character encoding with é read from csv file

Related 관련 기사

  1. 1

    Why can't I read whole file?

  2. 2

    How can I read a big CSV file without Memory exhausted?

  3. 3

    VB.NET read only some cells for each row from csv file

  4. 4

    Can't figure out the format this file is expecting to read

  5. 5

    Android - Java can't read the whole JSON file

  6. 6

    Can't import special characters from CSV file

  7. 7

    How can I read from a CSV file into 2 ArrayLists depending on the data type I have got in the File?

  8. 8

    Save copy of row in csv file

  9. 9

    table2CSV Skip first row

  10. 10

    Can't read an Excel file - File.open isn't valid

  11. 11

    Can't Find File When Using Scanner to read In a Previously Created File

  12. 12

    Select row to be deleted in csv file in python

  13. 13

    Importing a CSV file into postgres - skip the first line

  14. 14

    Read any column from csv file

  15. 15

    ExecuteScalar doesn't return the first column of the first row

  16. 16

    C++ File I/O--can't read/write simultaneously?

  17. 17

    Can't read file from secure_filename(f.filename)

  18. 18

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

  19. 19

    Can python read this type of config file?

  20. 20

    how can read files in directory and write to file

  21. 21

    can people read the content of an online php file?

  22. 22

    Why does the cat command only read from the first file descriptor?

  23. 23

    cron can't read PYTHONPATH environment variables?

  24. 24

    Why email can't be read from Maildir?

  25. 25

    Can't overwrite file on Windows

  26. 26

    PHPexcel can't save file

  27. 27

    Can't find the file specified

  28. 28

    using textscan to read badly formatted CSV file in Octave

  29. 29

    How to fix character encoding with é read from csv file

뜨겁다태그

보관