简体   繁体   中英

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 :

 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. 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.

Edit.

You may also use str.replace method for improved performance as pointed out by zwer below.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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