如何从 CSV 导入并将包含的数字加在一起?

How to import from CSV and add containing numbers together?

我在读取 CSV 文件然后将包含的数字相加时遇到问题。

main.au3

$billsRawData1 = FileReadLine(@AppDataDir & "\testDir\test.csv", 2)
$billsArray1 = StringSplit($billsRawData1, ",")

$billsRawData2 = FileReadLine(@AppDataDir & "\testDir\test.csv", 3)
$billsArray2 = StringSplit($billsRawData2, ",")

$billsRawData3 = FileReadLine(@AppDataDir & "\testDir\test.csv", 4)
$billsArray3 = StringSplit($billsRawData3, ",")

$billsRawData4 = FileReadLine(@AppDataDir & "\testDir\test.csv", 5)
$billsArray4 = StringSplit($billsRawData4, ",")

$billsRawData5 = FileReadLine(@AppDataDir & "\testDir\test.csv", 6)
$billsArray5 = StringSplit($billsRawData5, ",")

$billsRawData6 = FileReadLine(@AppDataDir & "\testDir\test.csv", 7)
$billsArray6 = StringSplit($billsRawData6, ",")

$billsRawData7 = FileReadLine(@AppDataDir & "\testDir\test.csv", 8)
$billsArray7 = StringSplit($billsRawData7, ",")

$billsRawData8 = FileReadLine(@AppDataDir & "\testDir\test.csv", 9)
$billsArray8 = StringSplit($billsRawData8, ",")

$billsRawData9 = FileReadLine(@AppDataDir & "\testDir\test.csv", 10)
$billsArray9 = StringSplit($billsRawData9, ",")

$billsRawData10 = FileReadLine(@AppDataDir & "\testDir\test.csv", 11)
$billsArray10 = StringSplit($billsRawData10, ",")

$billsRawData11 = FileReadLine(@AppDataDir & "\testDir\test.csv", 12)
$billsArray11 = StringSplit($billsRawData11, ",")

$billsRawData12 = FileReadLine(@AppDataDir & "\testDir\test.csv", 13)
$billsArray12 = StringSplit($billsRawData12, ",")

$billsRawData13 = FileReadLine(@AppDataDir & "\testDir\test.csv", 14)
$billsArray13 = StringSplit($billsRawData13, ",")

$billsRawData14 = FileReadLine(@AppDataDir & "\testDir\test.csv", 15)
$billsArray14 = StringSplit($billsRawData14, ",")

$billsRawData15 = FileReadLine(@AppDataDir & "\testDir\test.csv", 16)
$billsArray15 = StringSplit($billsRawData15, ",")

$billsRawData16 = FileReadLine(@AppDataDir & "\testDir\test.csv", 17)
$billsArray16 = StringSplit($billsRawData16, ",")

$billsRawData17 = FileReadLine(@AppDataDir & "\testDir\test.csv", 18)
$billsArray17 = StringSplit($billsRawData17, ",")

$billsRawData18 = FileReadLine(@AppDataDir & "\testDir\test.csv", 19)
$billsArray18 = StringSplit($billsRawData18, ",")

$billsRawData19 = FileReadLine(@AppDataDir & "\testDir\test.csv", 20)
$billsArray19 = StringSplit($billsRawData19, ",")

$billsRawData20 = FileReadLine(@AppDataDir & "\testDir\test.csv", 21)
$billsArray20 = StringSplit($billsRawData20, ",")

$total = $billsArray1[3] + $billsArray2[3]; + $billsArray3[3] + $billsArray4[3] + $billsArray5[3] + $billsArray6[3] + $billsArray7[3] + $billsArray8[3] + $billsArray9[3] + $billsArray10[3] + $billsArray11[3] + $billsArray12[3] + $billsArray13[3] + $billsArray14[3] + $billsArray15[3]; + $billsArray16[3] + $billsArray17[3] + $billsArray18[3] + $billsArray19[3] + $billsArray20[3]
MsgBox(0, "", $total)

test.csv

,,,
10/04/2015, Internet, , Monthly
10/07/2015, Gas, , Monthly
10/01/2015, Cable, , Monthly
10/27/2015, Storage, , Monthly
10/30/2015,School,0 , Monthly
10/18/2015,Rent,0 ,Monthly

test.csv 包含可变数量的行。我正在尝试在第三列中添加值。我将每一行读入一个变量并将其拆分为一个数组 ($billsArray1[3] returns </code>)。然后我将这些变量加在一起,但是 returns <code>0 for $total.

1) 为什么 $total 返回 0 而不是总数?

2) 如何创建一个循环来分配变量(所以我不需要为每一行创建新变量,因为我不知道会有多少行)?注意*它总是跳过第一行,所以它从第 2 行开始。

3) 如何创建另一个循环将第三列中的数据加在一起?

我不会创建一个单独的数组。我只是遍历文件,获取第 3 个条目,去掉所有不是数字的内容(比如 $),然后将其添加到总数中...

$file = FileOpen(@AppDataDir & "\testDir\test.csv", 0)
$total = 0

While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop

    ;Get the 3rd "token" and strip everything that is not a number.
    $bill = StringRegExpReplace(StringSplit($line, ',')[3], "[^\d]", "")

    ;Add current bill to total.
    $total = $total + $bill
WEnd
FileClose($file)

MsgBox(0, "Total", $total)