Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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
Text 在python中从文本文件中删除引号_Text_Python 3.x - Fatal编程技术网

Text 在python中从文本文件中删除引号

Text 在python中从文本文件中删除引号,text,python-3.x,Text,Python 3.x,我必须从一个相对较大的文本文件中删除引号。我已经查看了可能与此匹配的问题,但仍然无法从文本文件的特定行中删除引号 这是文件中的短段的外观: 1 - "C1". E #1 - "C1". 2 - "C2". 1 2 E #2 - "C2". 我想要1-“C1”。将替换为c1,并将E#1-“c1”替换为E c1。我尝试在python中替换它们,但由于双“,我得到了一个错误 我试过: input=open('file.text', 'r') output=open(newfile.txt'

我必须从一个相对较大的文本文件中删除引号。我已经查看了可能与此匹配的问题,但仍然无法从文本文件的特定行中删除引号

这是文件中的短段的外观:

1 - "C1".

E #1 - "C1".

2 - "C2".

1

2

E #2 - "C2".
我想要
1-“C1”
。将替换为
c1
,并将
E#1-“c1”
替换为
E c1
。我尝试在python中替换它们,但由于双
,我得到了一个错误

我试过:

input=open('file.text', 'r')
output=open(newfile.txt','w')
clean==input.read().replace("1 - "C1".","c1").replace("E #1 - "C1"."," E c1")
output.write(clean)
我与sed进行了以下交流:

sed 's\"//g'<'newfile.txt'>'outfile.txt'.
sed's\”/g“outfile.txt”。

但还有一个语法错误

如果您确定要替换literal,则只需转义引号或使用单引号:
replace('1-“C1.”,“C1”)
等等(以及纠正一些语法错误)。但是,这只适用于第一行,因此没有必要读取整个文件。要做更明智的工作,您可以使用:


我首先尝试了从今天发布的另一个问题中学到的一些东西。这是input=open('file.text','r')。很抱歉,这个评论框在meoutput=open(newfile.txt','w')上翻了。我建议您删除评论,并将信息移到问题上。
with open('file.text') as input, open('newfile.txt','w') as output:
     for line in input:
         line = re.sub(r'^(?P<num>\d+) - "C(?P=num)".$',
                       lambda m: 'c' + m.groups()[0], line)
         line = re.sub(r'^E #(?P<num>\d+) - "C(?P=num)".$',
                       lambda m: 'E c' + m.groups()[0], line)
         output.write(line)
sed 's/"//g' newfile.txt > outfile.txt