Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中使用正则表达式解析跨多行的csv数据_Python_Regex_Csv - Fatal编程技术网

如何在python中使用正则表达式解析跨多行的csv数据

如何在python中使用正则表达式解析跨多行的csv数据,python,regex,csv,Python,Regex,Csv,我在一个较大的文本文件中有以下文本: CHANNEL = ['AngR', 'Time', 'Disp', 'Velo', & 'Acce', 'Load', 'Forc', 'BLod'] UNIT = ['deg', 's', 'ft', 'ft/s', 'ft/s^2', & 'N', 'lbf', 'N'] 我希望解析数据,这样我就有了通道和单元的独立列表 我可以使用以下脚本获取通道和单元: matches1 = re.findall(r"(\w+)\s

我在一个较大的文本文件中有以下文本:

CHANNEL = ['AngR', 'Time', 'Disp', 'Velo', &
    'Acce', 'Load', 'Forc', 'BLod']
UNIT = ['deg', 's', 'ft', 'ft/s', 'ft/s^2', &
    'N', 'lbf', 'N']
我希望解析数据,这样我就有了通道和单元的独立列表

我可以使用以下脚本获取通道和单元:

matches1 = re.findall(r"(\w+)\s*\=", filetext)  #CHANNEL, UNIT
但是,我很难将括号中的数据提取到列表中:

my_regex2 = re.escape(matches1[0])+r"\s*\=\s*\[['(\w+)',*\s*\&*\n]+\]"
matches2 = re.findall(my_regex2, filetext)
由于某种原因,这个结果给出了整个字符串

这个更为简略的形式只给了我csv中的第一项(即“AngR”代表频道)

任何帮助都将不胜感激


谢谢

这回答了你的问题吗?尽管这可能也是一个问题。与其使用多行正则表达式,为什么不在看到
&
的地方连接行呢?这是否回答了您的问题?尽管这可能也是一个问题。与其使用多行正则表达式,为什么不在看到
&
的地方连接行呢?
my_regex = re.escape(matches1[0])+r"\s*\=\s*\['(\w+)',*\s"  #['AngR'] from CHANNEL
matches = re.findall(my_regex, filetext)