简体   繁体   English

从文本文件中读取字符串列表,并删除多余的引号

[英]Read a list of strings from text file and remove the extra quotes

In my text file, I have a list of strings as follows : 在我的文本文件中,有一个字符串列表,如下所示:

['', '"0=SYSEV,1=APPEV,2:3=VECEV"', '"ASEN"+$y', '"FALSE"', '"G"+$x+"ARBCFG"', '"G"+$x+"ARBPR"', '"HUGO:SECURE"', '"Internal"', '"SERIAL0:TRANSMIT"', '$fpi_mem_range', '$fpi_to_sri_base', '$fpi_to_sri_range', '$sx_fpi_base', '$sx_fpi_range', '$sx_sri_dest', '$trignum_g-1', '$x!=0', '$x!=1', '$x==1', '0', '0x0', '0x00', '0x0000', '0x00000000', '0x00000FFFF', '0x0000FFFF', '0x0D', '0x10', '0x1000', '0x10000000', '0x11001111', '0x11111100', '0xffc', '0xffffffff', '1', '1 clock cycle for generating the MSB', '10', '100', '101', '102', '103', '104', '115', '1156', '116', '117', '118', '1188', '119', '1192', '1196', '12', '120']

This list is written in text file using this code : 此列表使用以下代码写在文本文件中:

thefile = open('test.txt', 'w')
for item in thelist:
    thefile.write("%s\n" % item)

I want to read the list again. 我想再次阅读清单。 So I am using this code : 所以我正在使用此代码:

with open('test.txt') as f:
    content = f.readlines()
content = [x.strip() for x in content] 

The list that I am obtaining is correct but the extracted strings contain extra quotes that I want to remove. 我获得的列表是正确的,但提取的字符串包含我要删除的多余引号。 This is the list that I obtained : 这是我获得的清单:

['','"0=SYSEV,1=APPEV,2:3=VECEV"','"ASEN"+$y','"FALSE"',....,'0x0000FFFF']

To remove the extra quotes, I used ast.literaleval() but I got this error : 为了删除多余的引号,我使用了ast.literaleval(),但是出现了这个错误:

 File "/home/ubuntu/anaconda3/lib/python3.6/ast.py", line 35, in parse
 return compile(source, filename, mode, PyCF_ONLY_AST)

 File "<unknown>", line 1
 "ASEN"+$y

  ^
 SyntaxError: unexpected EOF while parsing

It seems that it removes the single quotes for all elements of the list even the one that we don't need to remove their quotes. 似乎它删除了列表中所有元素的单引号,甚至是我们不需要删除其引号的单引号。 Any better ideas ? 还有更好的主意吗?

A possible solution is to use re.sub to remove all double quote characters. 一个可能的解决方案是使用re.sub删除所有双引号字符。 Effectively this is done by matching the double quotes characters using regular expressions, and substituting an empty character instead. 实际上,这是通过使用正则表达式匹配双引号字符并替换为空字符来完成的。

import re
thelist = ['','"0=SYSEV,1=APPEV,2:3=VECEV"','"ASEN"+$y','"FALSE"','0x0000FFFF']

newlist = [];

for item in thelist:
    newlist.append(re.sub('["]','',item))

newlist will contain the elements from thelist without double quotes. newlist将包含列表中的元素, thelist带双引号。

Edit. 编辑。

You may also use str.replace method for improved performance as pointed out by zwer below. 您还可以使用str.replace方法来提高性能,如下面的zwer所指出的。

for item in thelist:
  newlist.append(item.replace('"',''))

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

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