解析JSX文件以提取Import语句的属性 [英] Parse a JSX file to extract the attributes of the Import statement

查看:81
本文介绍了解析JSX文件以提取Import语句的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个包含内容的jsx文件

There is a jsx file with contents

<import name="abcd" color="green" age="25" />
<View color={dsdssd}>
    <IBG
        color={[color.imagecolor, color.image125]}
        imageStyle={[styles.imageStyle, styles.image125]}
        source={{ uri: contents.aimeecard }} >
        <View color={styles.titleContainer}>
            <Text color={[{green: 45}, styles.mainTileText]}</Text>
            <View color={[abcde.text]} />
        </View>
</View>

我需要使用python脚本获取第一行的详细信息: 预期产量 name ="abcd" color =绿色"; 年龄="25"

I need to fetch the details of first line using python script: Expected output name="abcd" color="green" age="25"

jsx文件的路径也通过列表传递 例如:[abcd/file1.jsx,dcef/file2.jsx]

Also the path of jsx file is passed through list ex: [abcd/file1.jsx , dcef/file2.jsx]

Python代码尝试通过列表获取jsx文件

Python code tried for fetching jsx file through the list

for file in jsx_path:
   data = md.parse("file")
   print( file.firstChild.tagName )

未获取值并出现错误.

有人可以帮助我解决这个问题吗?

Can anyone help me in resolving this?

推荐答案

假定jsx_path是包含jsx文件所有路径的列表,您可以遍历每个文件并使用上下文管理器避免显式关闭类似所以:

Assuming jsx_path is the list containing all the paths to the jsx files, you can iterate over each and use a context manager to avoid closing explicitly the files like so:

data = ""

for file in jsx_path:
    with open(file) as f:
        data += f.readline()[8:-4] + "\n"

print(data)  # name="abcd" color="green" age="25"

在注释之后,如果要将其作为字典输出,则可以调整以前的代码:

Following your comment, if you want to output it as a dict, you can tweak the previous code:

import re

data = []

for file in jsx_path:
    with open(file) as f:
        data.append(re.split('\W+|=', f.readline()[8:-4]))

data_dict = []

for d in data:
   data_dict.append({key:value for (key, value) in zip(d[::2], d[1::2])})

print(data_dict)  # {'name': 'abcd', 'color': 'green', 'age': '25'}

请注意,这是黑客.我只按顺序读取JSX文件,因为您的用例足够简单.您还可以通过扩展stlib类HTMLParser:

Note that this is a hack. I only read the JSX file sequentially because your use case is simple enough to do so. You can also use a dedicated parser by extending the stlib class HTMLParser:

from html.parser import HTMLParser

class JSXImportParser(HTMLParser):

    def handle_starttag(self, tag, attrs):
        if tag == "import":
            self._import_attrs = {key:value for (key, value) in attrs}

    @property
    def import_attrs(self):
        return self._import_attrs


parser = JSXImportParser()
data = []

for file in jsx_path:
    with open(file) as f:
        parser.feed(f.read())
        data.append(parser.import_attrs)
        print(data)  # [{'name': 'abcd', 'color': 'green', 'age': '25'}]

请注意,这仅提取每个文件中最后一个导入标签的详细信息,您可以通过调整_import_attrs class属性来更改此行为.

Note that this only extracts the details of the last import tag in each file, you can alter this behavior by tweaking the _import_attrs class attribute.

在对使用XML解析器库的要求发表其他评论之后,使用ElementTree可以通过对文件进行采样以仅提取您感兴趣的内容(导入标记)来实现相同的目的:

Following your additional comment about the requirement to use an XML parser library, the same thing can be achieved using ElementTree by sampling the file to extract only what's interesting for you (the import tag):

import xml.etree.ElementTree as ET

data = []

for file in jsx_path:
    with open(file) as f:
        import_statement = ET.XML(f.readline())
        data.append(import_statement.attrib)

print(data)  # [{'name': 'abcd', 'color': 'green', 'age': '25'}]

当然,这仅在import语句位于第一行时才有效,如果不是这种情况,则必须在调用ET.XML之前首先找到它.

Of course this only works if the import statement is on the first line, if it's not the case, you'll have to locate it first before calling ET.XML.

这篇关于解析JSX文件以提取Import语句的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆