如何转换';s';在python中将列表中的字符串设置为0?
这是一个后续问题。你需要阅读来理解这里的问题 要这样做, 输入1(文本文件1) 输入2(文本文件2) 输出如何转换';s';在python中将列表中的字符串设置为0?,python,Python,这是一个后续问题。你需要阅读来理解这里的问题 要这样做, 输入1(文本文件1) 输入2(文本文件2) 输出 Item=Q232; Var:KLOM read_Var:read_q232: Get_Data: n: 00000000011111111001111111ssss111111111111 Item=Q130; Var:ABCD read_Var:read_q150: Get_Data: n: 000111111111111111101111111111111sss11
Item=Q232; Var:KLOM
read_Var:read_q232: Get_Data: n: 00000000011111111001111111ssss111111111111
Item=Q130; Var:ABCD
read_Var:read_q150: Get_Data: n: 000111111111111111101111111111111sss111111
Item=Q140; Var:FGDE
read_Var:read_q170: Get_Data: n: 01010101011111111sssssss111111111111111111
.. ..
.. ..
.. ..
Item=Q232; Data:KLOM
read_Data:burn_q232: Get_Data: n: 000000000111111110011111110000111111111111
Item=Q130; Data:ABCD
read_Data:burn_q150: Get_Data: n: 000111111111111111101111111111111000111111
Item=Q140; Data:FGDE
read_Data:burn_q170: Get_Data: n: 010101010111111110000000111111111111111111
.. ..
.. ..
.. ..
第3列input2文本文件中的“ssss”字符串替换为“0000”,并作为输出文件第二段中的输出写入
这是我修改的代码部分:
with open('output.txt', 'w') as fobj:
for key, values in arrays:
item = ids[key]
new_values = string.replace(values[3:],'s','0')
fobj.write('Item={}; Var:{}\n'.format(item, key))
fobj.write('read_Var:read_{}: Get_Data: {}\n\n'.format(item.lower(),
':'.join(new_values)))
但我有一个错误:
AttributeError: 'list' object has no attribute 'replace'
什么是
字符串
?无论如何,这应该是你想要的
with open('output.txt', 'w') as fobj:
for key, values in arrays:
item = ids[key]
new_values = values[-1].replace('s','0')
fobj.write('Item={}; Var:{}\n'.format(item, key))
fobj.write('read_Var:read_{}: Get_Data: {}\n\n'.format(item.lower(),':'.join(new_values)))
字符串是用于字符串操作的python模块,但是您的错误是list
。所以我假设字符串包含某种列表
如果有,您可能需要查看str
。等效的语法是
new_values = str.replace(values[-1],'s','0')
什么是
字符串
?无论如何,这应该是你想要的
with open('output.txt', 'w') as fobj:
for key, values in arrays:
item = ids[key]
new_values = values[-1].replace('s','0')
fobj.write('Item={}; Var:{}\n'.format(item, key))
fobj.write('read_Var:read_{}: Get_Data: {}\n\n'.format(item.lower(),':'.join(new_values)))
字符串是用于字符串操作的python模块,但是您的错误是list
。所以我假设字符串包含某种列表
如果有,您可能需要查看str
。等效的语法是
new_values = str.replace(values[-1],'s','0')
您需要获取列表中的最后一个条目
值
,并对其进行替换:
values[-1] = values[-1].replace('s', '0')
整个循环:
with open('output.txt', 'w') as fobj:
for key, values in arrays:
values[-1] = values[-1].replace('s', '0')
item = ids[key]
fobj.write('Item={}; Var:{}\n'.format(item, key))
fobj.write('read_Var:read_{}: Get_Data: {}\n\n'.format(item.lower(),
':'.join(values)))
输出:
Item=Q232; Var:KLOM
read_Var:read_q232: Get_Data: n:000000000111111110011111110000111111111111
Item=QI30; Var:ABCD
read_Var:read_qi30: Get_Data: n:000111111111111111101111111111111000111111
Item=Q140; Var:FGDE
read_Var:read_q140: Get_Data: n:010101010111111110000000111111111111111111
您需要获取列表中的最后一个条目
值
,并对其进行替换:
values[-1] = values[-1].replace('s', '0')
整个循环:
with open('output.txt', 'w') as fobj:
for key, values in arrays:
values[-1] = values[-1].replace('s', '0')
item = ids[key]
fobj.write('Item={}; Var:{}\n'.format(item, key))
fobj.write('read_Var:read_{}: Get_Data: {}\n\n'.format(item.lower(),
':'.join(values)))
输出:
Item=Q232; Var:KLOM
read_Var:read_q232: Get_Data: n:000000000111111110011111110000111111111111
Item=QI30; Var:ABCD
read_Var:read_qi30: Get_Data: n:000111111111111111101111111111111000111111
Item=Q140; Var:FGDE
read_Var:read_q140: Get_Data: n:010101010111111110000000111111111111111111