简体   繁体   English

文本文件和 json 文件可以互换使用吗? 如果是这样,我如何在 python 中使用它?

[英]Can a text file and a json file be used interchangeably? And if so how can I use it in python?

Question: I was wondering if JSON and txt files could be used interchangeably in python.问:我想知道 JSON 和 txt 文件是否可以在 python 中互换使用。

More Details: I found this on the internet and this on stack overflow to find what a JSON file is but it did not say if json and txt could be used interchangeably ie using the same commands.更多详细信息:我发现这个在互联网上, 对堆栈溢出找到一个JSON文件是什么,但它并没有说,如果JSON和TXT可以互换,即使用相同的命令一起使用。 For example, can both use the same code with open('filename')as file: or does JSON require a different code.例如,可以在with open('filename')as file:使用相同的代码with open('filename')as file:或者 JSON 是否需要不同的代码。 Also if they can be used in the same general manner is linking and using commands for a JSON file and a txt file the same process?此外,如果它们可以以相同的一般方式使用,是否可以将 JSON 文件和 txt 文件的命令链接和使用相同的过程?

OS: windows 10操作系统: Windows 10

IDE: IDLE 64-bit IDE:空闲 64 位

Version: Python 3.7版本: Python 3.7

A .txt file can contain JSON data, and using open() in Python can open any file, with any content, and any file extension (granted the user running the code has permissions to do so) .txt文件可以包含 JSON 数据,在 Python 中使用open()可以打开任何文件、任何内容和任何文件扩展名(授予运行代码的用户这样做的权限)

It's not until you try to load a non JSON string or file using json.loads or json.load , respectively, where the problem starts.直到您尝试分别使用json.loadsjson.load加载非 JSON 字符串或文件,问题才开始。

In other words, a file contains binary data.换句话说,文件包含二进制数据。 The data can be represented as a string, that string could be XHTML, JSON, CSV, YAML, whatever, and you must use the appropriate parser to extract the relevant data from that format (but it's not always the file extensions that determine what to use)数据可以表示为一个字符串,该字符串可以是 XHTML、JSON、CSV、YAML 等等,您必须使用适当的解析器从该格式中提取相关数据(但并不总是文件扩展名决定要使用什么格式)用)

does JSON require a different code JSON 是否需要不同的代码

It requires another module它需要另一个模块

import json 
with open(name) as f:
    data = json.load(f) 

You can read the raw data out of any file the same way;你可以用同样的方式从任何文件中读取原始数据; the difference is in reading the structure in the data.区别在于读取数据中的结构。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 重新格式化文本文件,以便它可以在 python 中使用 numpy 数组? - Reformat a text file so it can be used a numpy array, in python? shapiro 和 normaltest 在 python 中可以互换使用吗? - Can shapiro and normaltest be used interchangeably in python? 如何将文本文件转换为要在 Python 中使用的字典? - How can I convert a text file to a dictionary to be used in Python? 我如何使用其他文件并使它在其他文件中使用? (蟒蛇) - How can i use a different file and make it used in a other ?? (Python) 如何正确设置 json 配置文件,以便它可以用于替换 python 程序中的硬编码值 - How to properly set up a json config file so that it can be used to replace hardcoded values in python program 如何从文本文件中获取每一行并将其拆分,以便我可以在 python 中单独使用它们 - How do I get each line from a text file and split it so that I can use them all separately in python 如何使用while循环,以便用户可以打开新的文本文件? 蟒蛇 - How can you use a while loop so the user can open a new text file? Python 如何使用 JSON 文件作为使用 Python 的 MS Azure 文本分析的输入? - How can I use a JSON file as an input to MS Azure text analytics using Python? 如何将更多 json 数据添加到 python 中的文本文件? - How can i add more json data to a text file in python? 转换tsv文件,以便我可以将其用于python中的节点和边缘 - Convert tsv file so i can use it for nodes and edges in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM