如何逐行循环遍历文本文件,并将每一行附加到数组?
How do I loop though a text file, line by line, and append each line to an array?
我正在尝试逐行遍历 .txt,但似乎没有执行此操作的好方法。
想法是提取每一行并将其附加到数组。
假设你有一个像
这样的文本文件
并且我正在尝试从 blob 存储中读取同一个文件。为了将每一行变成一个数组,您可以使用一个带有 split() 函数的组合连接器。这是我的逻辑应用程序屏幕截图以供参考:-
这里是 compose 连接器中的拆分表达式
split(body('Get_blob_content_(V2)'),'
')
结果
下面是我的逻辑应用程序的代码视图:
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Compose": {
"inputs": "@split(body('Get_blob_content_(V2)'),'\r\n')",
"runAfter": {
"Get_blob_content_(V2)": [
"Succeeded"
]
},
"type": "Compose"
},
"Get_blob_content_(V2)": {
"inputs": {
"host": {
"connection": {
"name": "@parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "get",
"path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/files/@{encodeURIComponent(encodeURIComponent('JTJmY29udGFpbmVyMSUyZnNhbXBsZVRleHRGaWxlLnR4dA=='))}/content"
},
"metadata": {
"JTJmY29udGFpbmVyMSUyZnNhbXBsZVRleHRGaWxlLnR4dA==": "/container1/sampleTextFile.txt"
},
"runAfter": {},
"type": "ApiConnection"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"manual": {
"inputs": {},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {
"$connections": {
"value": {
"azureblob": {
"connectionId": "/subscriptions/<YourSubscriptionId>/resourceGroups/<YourResourceGroup>/providers/Microsoft.Web/connections/azureblob",
"connectionName": "azureblob",
"id": "/subscriptions/<YourSubscriptionId>/providers/Microsoft.Web/locations/northcentralus/managedApis/azureblob"
}
}
}
}
}
我正在尝试逐行遍历 .txt,但似乎没有执行此操作的好方法。
想法是提取每一行并将其附加到数组。
假设你有一个像
这样的文本文件并且我正在尝试从 blob 存储中读取同一个文件。为了将每一行变成一个数组,您可以使用一个带有 split() 函数的组合连接器。这是我的逻辑应用程序屏幕截图以供参考:-
这里是 compose 连接器中的拆分表达式
split(body('Get_blob_content_(V2)'),'
')
结果
下面是我的逻辑应用程序的代码视图:
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Compose": {
"inputs": "@split(body('Get_blob_content_(V2)'),'\r\n')",
"runAfter": {
"Get_blob_content_(V2)": [
"Succeeded"
]
},
"type": "Compose"
},
"Get_blob_content_(V2)": {
"inputs": {
"host": {
"connection": {
"name": "@parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "get",
"path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/files/@{encodeURIComponent(encodeURIComponent('JTJmY29udGFpbmVyMSUyZnNhbXBsZVRleHRGaWxlLnR4dA=='))}/content"
},
"metadata": {
"JTJmY29udGFpbmVyMSUyZnNhbXBsZVRleHRGaWxlLnR4dA==": "/container1/sampleTextFile.txt"
},
"runAfter": {},
"type": "ApiConnection"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"manual": {
"inputs": {},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {
"$connections": {
"value": {
"azureblob": {
"connectionId": "/subscriptions/<YourSubscriptionId>/resourceGroups/<YourResourceGroup>/providers/Microsoft.Web/connections/azureblob",
"connectionName": "azureblob",
"id": "/subscriptions/<YourSubscriptionId>/providers/Microsoft.Web/locations/northcentralus/managedApis/azureblob"
}
}
}
}
}