在 python 脚本中正确使用拆分
Using split in python script correctly
我正在制作一个程序,将 .m 文件解析为 .h 文件,并以不同的顺序重新排列一些内容。我基本上是 Python 的新手,但我知道其他语言。程序代码为:
with open('D:\\toolchain\Simulink\data\application_parameters_data.m','r') as InputFile:
for j in InputFile:
for k in range(2): InputFile.readline()
with open('D:\\toolchain\Connector\ConDataGenerated.h', 'w') as file1:
t1 = Template('$datatype $name $initValue $comment')
for i in InputFile:
file1.write("\n")
Type = [name.strip() for name in i.split(';\n')[0].split(',')]
file1.write(t1.safe_substitute(datatype = Type[5], name = Type[0], initValue =Type[1], comment =Type[4]))
我需要更改它的内容除以,在元素和之间;相应地在行之间。输入代码示例:
'ManCtrlInputShaftSpeedSwt', 0, [0 1], '', 'comment', 'uint16';
'ManCtrlInputShaftSpeedPar', 0, [0 1], '', 'comment', 'single';
'ManCtrlInputShaftSpeed1Swt', 0, [0 1], '', 'comment', 'uint16';
'ManCtrlInputShaftSpeed1Par', 0, [0 1], '', 'comment', 'single';
'ManCtrlInputShaftSpeed2Swt', 0, [0 1], '', 'comment', 'uint16';
'ManCtrlInputShaftSpeed2Par', 0, [0 1], '', 'comment', 'single';
'ManCtrlOutputShaftSpeedSwt', 0, [0 1], '', 'comment', 'uint16';
'ManCtrlOutputShaftSpeedPar', 0, [0 1], '', 'comment', 'single';
'ManCtrlOutputShaftSpeedDirnSwt', 0, [0 1], '', 'comment', 'uint16';
'ManCtrlOutputShaftSpeedDirnPar', 0, [0 1], '', 'comment', 'uint16';
'ManCtrlMeasPositionS1Swt', 0, [0 1], '', 'comment', 'uint16';
'ManCtrlMeasPositionS1Par', 0, [0 1], '', 'comment', 'single';
事情是这样的。脚本工作正常,直到它遇到字符串之间的 space 或它无法识别的东西,或者它只将字符串的第一个元素写入新文件,在这种情况下它会毫无问题地遍历字符串。我认为它以某种方式连接到编译器告诉我接下来:
Traceback (most recent call last):
File "D:\StringTemplate.py", line 16, in <module>
file1.write(t1.safe_substitute(datatype = Type[5], name = Type[0], initValue =Type[1], comment =Type[4]))
IndexError: list index out of range
即使创建的文件存在此问题。输出代码示例:
'uint16' 'ManCtrlInputShaftSpeedSwt' 0 'comment'
'single' 'ManCtrlInputShaftSpeedPar' 0 'comment'
'uint16' 'ManCtrlInputShaftSpeed1Swt' 0 'comment'
'single' 'ManCtrlInputShaftSpeed1Par' 0 'comment'
'uint16' 'ManCtrlInputShaftSpeed2Swt' 0 'comment'
'single' 'ManCtrlInputShaftSpeed2Par' 0 'comment'
'uint16' 'ManCtrlOutputShaftSpeedSwt' 0 'comment'
'single' 'ManCtrlOutputShaftSpeedPar' 0 'comment'
'uint16' 'ManCtrlOutputShaftSpeedDirnSwt' 0 'comment'
'uint16' 'ManCtrlOutputShaftSpeedDirnPar' 0 'comment'
我该怎么办?
您可以添加一个条件来检查该行的长度是否为 5 / 如果它不为空,例如:
Type = [name.strip() for name in i.split(';\n')[0].split(',')]
if len(Type) > 5:
file1.write(t1.safe_substitute(datatype = Type[5], name = Type[0], initValue =Type[1], comment =Type[4]))
我正在制作一个程序,将 .m 文件解析为 .h 文件,并以不同的顺序重新排列一些内容。我基本上是 Python 的新手,但我知道其他语言。程序代码为:
with open('D:\\toolchain\Simulink\data\application_parameters_data.m','r') as InputFile:
for j in InputFile:
for k in range(2): InputFile.readline()
with open('D:\\toolchain\Connector\ConDataGenerated.h', 'w') as file1:
t1 = Template('$datatype $name $initValue $comment')
for i in InputFile:
file1.write("\n")
Type = [name.strip() for name in i.split(';\n')[0].split(',')]
file1.write(t1.safe_substitute(datatype = Type[5], name = Type[0], initValue =Type[1], comment =Type[4]))
我需要更改它的内容除以,在元素和之间;相应地在行之间。输入代码示例:
'ManCtrlInputShaftSpeedSwt', 0, [0 1], '', 'comment', 'uint16';
'ManCtrlInputShaftSpeedPar', 0, [0 1], '', 'comment', 'single';
'ManCtrlInputShaftSpeed1Swt', 0, [0 1], '', 'comment', 'uint16';
'ManCtrlInputShaftSpeed1Par', 0, [0 1], '', 'comment', 'single';
'ManCtrlInputShaftSpeed2Swt', 0, [0 1], '', 'comment', 'uint16';
'ManCtrlInputShaftSpeed2Par', 0, [0 1], '', 'comment', 'single';
'ManCtrlOutputShaftSpeedSwt', 0, [0 1], '', 'comment', 'uint16';
'ManCtrlOutputShaftSpeedPar', 0, [0 1], '', 'comment', 'single';
'ManCtrlOutputShaftSpeedDirnSwt', 0, [0 1], '', 'comment', 'uint16';
'ManCtrlOutputShaftSpeedDirnPar', 0, [0 1], '', 'comment', 'uint16';
'ManCtrlMeasPositionS1Swt', 0, [0 1], '', 'comment', 'uint16';
'ManCtrlMeasPositionS1Par', 0, [0 1], '', 'comment', 'single';
事情是这样的。脚本工作正常,直到它遇到字符串之间的 space 或它无法识别的东西,或者它只将字符串的第一个元素写入新文件,在这种情况下它会毫无问题地遍历字符串。我认为它以某种方式连接到编译器告诉我接下来:
Traceback (most recent call last):
File "D:\StringTemplate.py", line 16, in <module>
file1.write(t1.safe_substitute(datatype = Type[5], name = Type[0], initValue =Type[1], comment =Type[4]))
IndexError: list index out of range
即使创建的文件存在此问题。输出代码示例:
'uint16' 'ManCtrlInputShaftSpeedSwt' 0 'comment'
'single' 'ManCtrlInputShaftSpeedPar' 0 'comment'
'uint16' 'ManCtrlInputShaftSpeed1Swt' 0 'comment'
'single' 'ManCtrlInputShaftSpeed1Par' 0 'comment'
'uint16' 'ManCtrlInputShaftSpeed2Swt' 0 'comment'
'single' 'ManCtrlInputShaftSpeed2Par' 0 'comment'
'uint16' 'ManCtrlOutputShaftSpeedSwt' 0 'comment'
'single' 'ManCtrlOutputShaftSpeedPar' 0 'comment'
'uint16' 'ManCtrlOutputShaftSpeedDirnSwt' 0 'comment'
'uint16' 'ManCtrlOutputShaftSpeedDirnPar' 0 'comment'
我该怎么办?
您可以添加一个条件来检查该行的长度是否为 5 / 如果它不为空,例如:
Type = [name.strip() for name in i.split(';\n')[0].split(',')]
if len(Type) > 5:
file1.write(t1.safe_substitute(datatype = Type[5], name = Type[0], initValue =Type[1], comment =Type[4]))