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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (169)

我在阅读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,$40 , Monthly
10/07/2015, Gas,$80 , Monthly
10/01/2015, Cable,$60 , Monthly
10/27/2015, Storage,$50 , Monthly
10/30/2015,School,$150 , Monthly
10/18/2015,Rent,$750 ,Monthly

test.csv包含可变数量的行。我正在尝试在第三列中添加值。我将每一行读入一个变量并将其拆分为一个数组($billsArray1[3]返回$40)。我再加入这些变量在一起,但它返回0$total

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

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

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)

所属标签

可能回答问题的人

  • HKC

    红客学院 · 创始人 (已认证)

    26 粉丝7 提问5 回答
  • 西风

    renzha.net · 站长 (已认证)

    9 粉丝1 提问4 回答
  • Dingda

    Dingda · 站长 (已认证)

    4 粉丝0 提问3 回答
  • 螃蟹居

    1 粉丝0 提问2 回答

扫码关注云+社区

领取腾讯云代金券