从同一笔记本导入表

2024-04-20 05:34:52 发布

您现在位置:Python中文网/ 问答频道 /正文

如何从文本块中读取标记表并将其导入同一PowerShell笔记本中代码块中的变量

试图使用PowerShell导入

$b=Import-Csv -Path <> -Delimiter '|'

enter image description here

无法确定如何将-Path参数指向同一笔记本中的文本块。在Azure Data Studio中使用.ipynb文件


Tags: csvpath代码标记文本importdata参数
2条回答

我相信你正在寻找的功能是不可能的。作为一种解决方法,我建议将单元格标记作为变量存储在Pythonfirst中,并使用该变量填充打印的标记单元格。这里有一个例子。我相信它可以在任何基于iPython的笔记本电脑上运行:

#running this cell in your notebook will print the variable as Markdown
mymd = "# Some markdown"
from IPython.display import display, Markdown
display(Markdown(mymd))

更新:如果您担心表示多行降价太复杂,您有两个好的选择。首先,使用三倍引号读取作为字符串一部分的换行符:

mymd = """
| First Header  | Second Header |
|       - |       - |
| Content Cell  | Content Cell  |
| Content Cell  | Content Cell  |
"""

选项2:将您的标记放在文件中,并将其读取为字符串:

with open("somefile.md") as f:
    mymd = f.read()

这两个选项都将受益于一个有良好文档记录和仔细遵循的工作流,但对于这个用例来说都会很好地工作

根据对该问题的评论,^{}似乎包含JSON格式的文本。
Quote about ^{} from WikiPedia

JSON is an open-standard file format or data interchange format that uses human-readable text to transmit data objects consisting of attribute–value pairs and array data types (or any other serializable value). It is a very common data format, with a diverse range of applications, such as serving as replacement for XML in AJAX systems.

JSON is a language-independent data format. It was derived from JavaScript, but many modern programming languages include code to generate and parse JSON-format data. The official Internet media type for JSON is application/json. JSON filenames use the extension .json.

此外,PowerShell还有自己的“cmdlet”命令用于管理JSON文件:^{}^{}
ConvertFrom-Json(和ConvertTo-Json)cmdlet没有-Path参数,而是将从-InputObject变量或流转换,如果信息来自文件,则可以使用^{}cmdlet从文件检索数据:

$Data = Get-Content -Path .\YourFile.ipynb | ConvertFrom-Json

如果您的文件实际上不是通过文件系统提供的,而是来自internet上的网页(我怀疑是这样),那么您需要依赖^{}cmdlet,或者如果它涉及^{}cmdlet上的web API。对于这些cmdlet,您需要了解并提供更多详细信息,如需要寻址的URL

相关问题 更多 >