[英]To remove strings in between quotes and inserting a new line before a special character from a text file and saving it
I am trying to read a text file and remove anything in between quotes ("") and also any kind of commas and semi-colons. 我正在尝试读取文本文件,并删除引号(“”)之间的所有内容以及任何逗号和分号。
I also want to insert a line before the string of characters 'qwe'. 我还想在字符串'qwe'之前插入一行。
I have no experience with python and I have been tasked to this in python. 我没有使用python的经验,并且已经在python中受过这项工作。
f = open('filename.txt', 'r')
for line in f:
x=''
for i in range(len(line)):
if str(line[i]) != ',':
x += str(line[i])
if str(line[i]) == ',':
pass
print (x)
Here's some example code to read and remove comma's from a text file, you can start from this and try adding all the other stuff you want to do. 这是一些示例代码,用于从文本文件中读取和删除逗号,您可以从此开始并尝试添加所有您想做的其他事情。 Just have this code saved in the same place as the text file and type the text file's name where it says filename.
只需将此代码保存在与文本文件相同的位置,然后在显示文件名的地方键入文本文件的名称即可。
import re
with open('code.txt', 'r') as infile, open('noquotes.txt', 'w') as outfile:
for line in infile:
x=''
line=re.sub('".+?"', '', line)
for i in range(len(line)):
if str(line[i])!=',':
x+=str(line[i])
if str(line[i])== ',':
pass
outfile.write(x)
outfile.close(x)
I got it :D 我知道了:D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.