简体   繁体   English

删除引号之间的字符串,并在文本文件中的特殊字符之前插入新行并保存

[英]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.

相关问题 从文本文件中读取字符串列表,并删除多余的引号 - Read a list of strings from text file and remove the extra quotes 从文本文件中删除一行中的特定字符 - Remove specific character in a line from a text file 删除换行符但仅在引号之间 - Remove New Line Feed But Only Between Quotes 如何删除/替换字符串中第n个特殊字符之前和之后的字符串? - How to remove/replace strings before and after nth special character in a string? 使用Python从文本文件中删除行字符(有条件) - Remove line character from a text file using Python (With a condition) 如何从文本文件中打印字符串,在 Python 中以新行分隔 - How to print strings from a text file,separated by new line in Python 如何在特定字符之前从字符串中删除特殊字符? - How to remove special characters from a string before specific character? 如何在python的文本文件中插入另一行之前的特定行,而不在任何行之间插入任何空行? - how to insert an specific line before another line, into a text file with python and without inserting any empty lines between any lines? 文本文件中的Python奇怪的换行符 - Python weird new line character in text file 如何从Python中从文本文件读取的行中删除特殊字符和字母? - How to remove special characters and letters from a line read from a text file in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM