VB6 逗号分隔 CSV 文件
我试图分隔一个逗号分隔的文件,由于某种原因,我似乎没有得到我期望得到的输出。
这是代码:
strCSVPath = "E:\cfaApp\tester.csv"
int77 = FreeFile
Open strCSVPath For Input As #int77
Do Until EOF(int77)
Input #int77, strName, intHours, strMon, strTue, strWed, strThu, strFri, strSat
Debug.Print strName & vbCr & intHours
'Debug.Print strName & vbCr & intHours & vbCr & strMon & vbCr & strTue & vbCr & strWed & vbCr & strThu & vbCr & strFri & vbCr & strSat & vbCr
Loop
输出如下所示:
1.0-Store Manager (1 Employee)
11:00 AM-8:30 PM
10:00 AM-7:30 PM
1.1-Assistant Managers
Wood, Chris
上面的输出是错误的。这是 CSV 文件(其中一部分,不是全部)
1.0-Store Manager (1 Employee),,,,,,,,
"Pro, Bob",1.0-Store Manager,47.5,5:30 AM-3:00 PM,5:30 AM-3:00 PM,11:00 AM-8:30 PM,11:00 AM-8:30 PM,9:00 AM-6:30 PM,OFF
1.1-Assistant Managers (3 Employees),,,,,,,,
"Crow, Billy",1.1-Assistant Managers,47.5,10:00 AM-7:30 PM,5:30 AM-3:00 PM,5:30 AM-3:00 PM,5:30 AM-3:00 PM,OFF,11:00 AM-8:30 PM
"Ras, Pat",1.1-Assistant Managers,47.5,5:30 AM-3:00 PM,9:00 AM-6:30 PM,10:00 AM-7:30 PM,,11:00 AM-8:30 PM,9:00 AM-6:30 PM
"Wood, Chris",1.1-Assistant Managers,47.5,,11:00 AM-8:00 PM,8:30 AM-6:30 PM,10:00 AM-7:30 PM,5:30 AM-3:00 PM,5:30 AM-3:00 PM
1.2- Supervisors (7 Employees),,,,,,,,
如您所见,输出确实完全跳过了 Pro、Bob。然后在显示 Wood 之前跳过 Crow 和 Ras。
我也收到一个错误
Input past end of file
I am trying to separate a comma delimited file and for some reason I seem to not be getting the output that I was expecting to get.
Here is the code:
strCSVPath = "E:\cfaApp\tester.csv"
int77 = FreeFile
Open strCSVPath For Input As #int77
Do Until EOF(int77)
Input #int77, strName, intHours, strMon, strTue, strWed, strThu, strFri, strSat
Debug.Print strName & vbCr & intHours
'Debug.Print strName & vbCr & intHours & vbCr & strMon & vbCr & strTue & vbCr & strWed & vbCr & strThu & vbCr & strFri & vbCr & strSat & vbCr
Loop
The output looks like this:
1.0-Store Manager (1 Employee)
11:00 AM-8:30 PM
10:00 AM-7:30 PM
1.1-Assistant Managers
Wood, Chris
Above output is wrong. This is the CSV file (some of it, not all of it)
1.0-Store Manager (1 Employee),,,,,,,,
"Pro, Bob",1.0-Store Manager,47.5,5:30 AM-3:00 PM,5:30 AM-3:00 PM,11:00 AM-8:30 PM,11:00 AM-8:30 PM,9:00 AM-6:30 PM,OFF
1.1-Assistant Managers (3 Employees),,,,,,,,
"Crow, Billy",1.1-Assistant Managers,47.5,10:00 AM-7:30 PM,5:30 AM-3:00 PM,5:30 AM-3:00 PM,5:30 AM-3:00 PM,OFF,11:00 AM-8:30 PM
"Ras, Pat",1.1-Assistant Managers,47.5,5:30 AM-3:00 PM,9:00 AM-6:30 PM,10:00 AM-7:30 PM,,11:00 AM-8:30 PM,9:00 AM-6:30 PM
"Wood, Chris",1.1-Assistant Managers,47.5,,11:00 AM-8:00 PM,8:30 AM-6:30 PM,10:00 AM-7:30 PM,5:30 AM-3:00 PM,5:30 AM-3:00 PM
1.2- Supervisors (7 Employees),,,,,,,,
As you can see, the output does skips Pro, Bob altogether. And then skips Crow and Ras before displaying Wood.
I also get an error of
Input past end of file
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简而言之,Input # 并不是为读取 CSV 而设计的。请参阅 http://msdn.microsoft.com/en -us/library/aa243386(v=VS.60).aspx
可能有一个可以从 VB6 使用的外部 CSV 解析器,但我不知道有一个。您可以相当轻松地编写一个 CSV 解析器,一次获取一个字符并使用有限状态机。
In short, Input # is not designed to read CSV. See http://msdn.microsoft.com/en-us/library/aa243386(v=VS.60).aspx
There may be an external CSV parser you could use from VB6 but I don't know of one offhand. You could write a CSV parser fairly easily that gets one character at a time and uses a finite state machine.
我已经有一段时间没有使用VB6了,但我碰巧记得可以使用
ADODB.Recordset
对象来像 SQL 数据库一样查询 CSV 文件。以下是 Microsoft 文档,包括如何读取不带标题行的 CSV 文件 。
It's been a while since I have used VB6, but I happened to remember that it is possible to use the
ADODB.Recordset
object to query the CSV file like a SQL database.Here is Microsoft documentation, including how to read CSV files without header rows.
尝试使用其他分隔符,例如 ;或管道。
然后看看输出是否正常。
如果是,请尝试引用名称中的逗号或将名称设置在其他引号中,例如 '
玩得开心,
约翰
Try using a other separator like ; or Pipe.
Then see if the output is okay.
If YES try to quote the commas in the name or set the name in other quotes like '
Have Fun,
John