简体   繁体   English

如何从python中的文本文件创建一个类?

[英]How do I create a class from a text file in python?

How would i create a class from a Text_file.txt containing the below data.我将如何从包含以下数据的 Text_file.txt 创建一个类。 So that AI are my objects.所以人工智能是我的对象。

Text_file.txt format: text_file.txt 格式:

[{'A': 443582874, 'B': '015490199481', 'C': 98874007784, 'D': 'Banana', 'E': 'buy', 'F': '0.2000', 'G': '0.031076', 'H': '-0.000000621520', 'I': '2019-02-01T11:19:22.740Z'}, {'A': 443484705, 'B': '015490131470', 'C': 98848326070, 'D': 'Apple', 'E': 'sell', 'F': '0.2000', 'G': '0.030879', 'H': '-0.000000617580', 'I': '2019-02-01T09:35:09.198Z'}]

So that when:所以当:

print(class['D0']):

Returns:返回:

Banana

And

print(class['D0':'D1'])

Returns:返回:

Banana
Apples

This is not really a thing, but there is a way of doing it.这实际上不是一件事,但有一种方法可以做到。

If instead of it being text_file.txt the file was text_file.py and the contents were something like this:如果不是text_file.txt文件是text_file.py并且内容是这样的:

var = [{'A': 443582874, 'B': '015490199481', 'C': 98874007784, 'D': 'Banana', 'E': 'buy', 'F': '0.2000', 'G': '0.031076', 'H': '-0.000000621520', 'I': '2019-02-01T11:19:22.740Z'}, {'A': 443484705, 'B': '015490131470', 'C': 98848326070, 'D': 'Apple', 'E': 'sell', 'F': '0.2000', 'G': '0.030879', 'H': '-0.000000617580', 'I': '2019-02-01T09:35:09.198Z'}]

then in your script you had然后在你的脚本中你有

from text_file import var

print(var[0]['D'])

that would give you 'Banana' .那会给你'Banana'

You would need text_file.py to be in the same folder (or at least somewhere in your path) as your python script, so that it could be imported.您需要text_file.py与您的 python 脚本位于同一文件夹(或至少在您的路径中的某个位置),以便可以导入它。

I do this sometimes for configs.我有时会为配置执行此操作。 You are also mis-using the name class which isn't what this is.您还误用了名称class ,而这不是它的名称。


Edit:编辑:

doing this:这样做:

print(class['D0':'D1'])

is probably possible, but I don't think it's what you want at all - to do it you would basically be rebuilding your own indexing symantics for a different data structure - why not just learn the one that python already has?可能是可能的,但我认为这根本不是你想要的 - 要做到这一点,你基本上会为不同的数据结构重建自己的索引语义 - 为什么不学习 python 已经拥有的呢?

something like this:像这样:

print([i['D'] for i in var[0:1]])

would give you what you want会给你你想要的

class is a Python reserved word with a special meaning, which I don't think is what you mean here. class是一个具有特殊含义的 Python 保留字,我认为这不是您在这里的意思。

What you appear to have is a JSON string, which can be imported into Python as a 2-element list of dicts.您似乎拥有的是一个 JSON 字符串,它可以作为 2 元素的 dict 列表导入 Python。 Except it's using single-quotes instead of double-quotes.除了它使用单引号而不是双引号。 So read the file into string , then所以将文件读入string ,然后

import json

string = string.replace( "'", '"')
j = json.loads( string)

you can now refer to j[0]['D'] and [ x['D'] for x in j ] (the latter is the list ['Banana', 'Apple']您现在可以参考j[0]['D'][ x['D'] for x in j ] (后者是列表['Banana', 'Apple']

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM