Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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
Python清理数据_Python_Regex - Fatal编程技术网

Python清理数据

Python清理数据,python,regex,Python,Regex,我正试图从一个大的文本文件(大约3GB)中删除不需要的字符。我试图删除整个文件中不需要的前斜杠和反斜杠。我想在两个单词之间保留波浪线,它充当分隔符。文件的格式如下所示: Cornet~Chris Tyle Cornet\~Warren Vache Cornet~Roger Webster Cornet~\Grimethorpe Coll//iery Band Cornet/~Daniel Rollston Cornet~Murley Silver Band Chocolate~Chocolate

我正试图从一个大的文本文件(大约3GB)中删除不需要的字符。我试图删除整个文件中不需要的前斜杠和反斜杠。我想在两个单词之间保留波浪线,它充当分隔符。文件的格式如下所示:

Cornet~Chris Tyle
Cornet\~Warren Vache
Cornet~Roger Webster
Cornet~\Grimethorpe Coll//iery Band
Cornet/~Daniel Rollston
Cornet~Murley Silver Band
Chocolate~Chocolate liquor
Chocolate~Theobroma cacao
Chocolate~Meso/america
Chocolate~
Chocolate~Theobroma cacao
         ~Mesoamerica
因此,在上面的示例中,我希望删除所有后斜杠/前斜杠,以便文字可读(同时保留波浪线)。我会为此使用Python正则表达式吗?另一种可能是删除包含斜杠的行,但我想把它作为最后的手段


**编辑:对不起,忘了提一件事!有些行如下所示:

Cornet~Chris Tyle
Cornet\~Warren Vache
Cornet~Roger Webster
Cornet~\Grimethorpe Coll//iery Band
Cornet/~Daniel Rollston
Cornet~Murley Silver Band
Chocolate~Chocolate liquor
Chocolate~Theobroma cacao
Chocolate~Meso/america
Chocolate~
Chocolate~Theobroma cacao
         ~Mesoamerica
除了删除前斜杠和后斜杠之外,我还必须删除平铺之前或之后的所有空行**


谢谢你的帮助

像这样简单的事情行吗?(我相信f中的
行是一个生成器。无论哪种方式,它一次只能读取一行)

更新(OP还希望“删除波浪线之前或之后的所有空行”)

这将跳过波浪线前面有空格的行,并替换其余部分的前斜杠和后斜杠:

import re

pattern = re.compile(r'\s+~')

with open(filename, "r") as f: 
    for line in f: 
        if not pattern.match(line):
            line = line.replace("/", "") 
            line = line.replace("\\", "")
            print line

注意:如果你真正想要的只是将所有行保留为“word+tilde+word”格式,请删除斜杠并放弃所有其他内容,说明这样会更容易理解。

像这样简单的事情可以吗?(我相信f中的
行是一个生成器。无论哪种方式,它一次只能读取一行)

更新(OP还希望“删除波浪线之前或之后的所有空行”)

这将跳过波浪线前面有空格的行,并替换其余部分的前斜杠和后斜杠:

import re

pattern = re.compile(r'\s+~')

with open(filename, "r") as f: 
    for line in f: 
        if not pattern.match(line):
            line = line.replace("/", "") 
            line = line.replace("\\", "")
            print line

注意:如果您真正想要的只是将所有行保留为“word+tilde+word”格式,请删除斜杠并放弃所有其他内容,说明这将更容易理解。

简单,只需使用
str.replace()

请注意双反斜杠
\\
,它并没有取代双反斜杠,而是一个反斜杠正在转义另一个反斜杠

代码:

def clean():
    with open('example.txt', 'r') as f:
        outputs = []
        for line in f:
            output = line.replace('/', '')
            output = output.replace('\\', '')
            output = output.replace('\n', '')
            outputs.append(output)
    return outputs

print(clean())
['Cornet~Chris Tyle', 'Cornet~Warren Vache', 'Cornet~Roger Webster', 'Cornet~Grimethorpe Colliery Band', 'Cornet~Daniel Rollston', 'Cornet~Murley Silver Band', 'Chocolate~Chocolate liquor', 'Chocolate~Theobroma cacao', 'Chocolate~Mesoamerica']
输出:

def clean():
    with open('example.txt', 'r') as f:
        outputs = []
        for line in f:
            output = line.replace('/', '')
            output = output.replace('\\', '')
            output = output.replace('\n', '')
            outputs.append(output)
    return outputs

print(clean())
['Cornet~Chris Tyle', 'Cornet~Warren Vache', 'Cornet~Roger Webster', 'Cornet~Grimethorpe Colliery Band', 'Cornet~Daniel Rollston', 'Cornet~Murley Silver Band', 'Chocolate~Chocolate liquor', 'Chocolate~Theobroma cacao', 'Chocolate~Mesoamerica']

简单,只需使用
str.replace()

请注意双反斜杠
\\
,它并没有取代双反斜杠,而是一个反斜杠正在转义另一个反斜杠

代码:

def clean():
    with open('example.txt', 'r') as f:
        outputs = []
        for line in f:
            output = line.replace('/', '')
            output = output.replace('\\', '')
            output = output.replace('\n', '')
            outputs.append(output)
    return outputs

print(clean())
['Cornet~Chris Tyle', 'Cornet~Warren Vache', 'Cornet~Roger Webster', 'Cornet~Grimethorpe Colliery Band', 'Cornet~Daniel Rollston', 'Cornet~Murley Silver Band', 'Chocolate~Chocolate liquor', 'Chocolate~Theobroma cacao', 'Chocolate~Mesoamerica']
输出:

def clean():
    with open('example.txt', 'r') as f:
        outputs = []
        for line in f:
            output = line.replace('/', '')
            output = output.replace('\\', '')
            output = output.replace('\n', '')
            outputs.append(output)
    return outputs

print(clean())
['Cornet~Chris Tyle', 'Cornet~Warren Vache', 'Cornet~Roger Webster', 'Cornet~Grimethorpe Colliery Band', 'Cornet~Daniel Rollston', 'Cornet~Murley Silver Band', 'Chocolate~Chocolate liquor', 'Chocolate~Theobroma cacao', 'Chocolate~Mesoamerica']
尝试:

此代码正在从输入文件中删除
\/
和以tilda开头或结尾的行
~
,并写入已清理的输出文件

基于输入文本,它将发出

Cornet~Chris Tyle
Cornet~Warren Vache
Cornet~Roger Webster
Cornet~Grimethorpe Colliery Band
Cornet~Daniel Rollston
Cornet~Murley Silver Band
Chocolate~Chocolate liquor
Chocolate~Theobroma cacao
Chocolate~Mesoamerica
Chocolate~Theobroma cacao
尝试:

此代码正在从输入文件中删除
\/
和以tilda开头或结尾的行
~
,并写入已清理的输出文件

基于输入文本,它将发出

Cornet~Chris Tyle
Cornet~Warren Vache
Cornet~Roger Webster
Cornet~Grimethorpe Colliery Band
Cornet~Daniel Rollston
Cornet~Murley Silver Band
Chocolate~Chocolate liquor
Chocolate~Theobroma cacao
Chocolate~Mesoamerica
Chocolate~Theobroma cacao


Python在这方面做得太过火了,它需要,比如。。。五行
tr-d/\\clean.txt
我知道你的意思。如果不是必须的话,我就不会使用python了!针对新要求更新:
tr-d/\clean.txt
。我不明白为什么你必须使用Python,除非它是家庭作业;而且homeworks通常不涉及3Gb文件…@Amadan Windows有
tr
吗?或者你只是假设我们都在linux上吗?:)@jDo:,我假设人们使用的是*nix,而不是Linux(我主要使用的是OSX),因为在上面开发Windows太痛苦了:PPython在这方面做得太过分了,它需要,比如。。。五行
tr-d/\\clean.txt
我知道你的意思。如果不是必须的话,我就不会使用python了!针对新要求更新:
tr-d/\clean.txt
。我不明白为什么你必须使用Python,除非它是家庭作业;而且homeworks通常不涉及3Gb文件…@Amadan Windows有
tr
吗?或者你只是假设我们都在linux上吗?:)@jDo:,我假设人们使用的是*nix,而不是Linux(我主要使用的是OSX),因为在上面开发Windows太痛苦了:PHow您会在不读取整个文件的情况下执行此操作吗?还有其他解决方案(迭代文件并以某种方式写出)?只是好奇而已。编辑:对不起,忘了提一件事!有些行是这样显示的:Chocolate~Chocolate~Theobroma cacao~中美洲除了删除前斜杠和后斜杠之外,我还必须删除瓷砖前后的所有空行。
f.read().split()
更糟糕,现在,内存中有一个3Gb字符串和一个数组。。。如果您必须使用Python,正确的方法是@jDo,一次读取一行。您确定要将3GB加载到内存中吗?试着用jDo回答的方法+1对于split,如果文本被遗漏,它可能会破坏文本。因此,如果我想替换两个反斜杠,我必须放置三个反斜杠?在不读取整个文件和任何其他解决方案(迭代文件并以某种方式写出)的情况下,如何做到这一点?只是好奇而已。编辑:对不起,忘了提一件事!有些行是这样显示的:Chocolate~Chocolate~Theobroma cacao~中美洲除了删除前斜杠和后斜杠之外,我还必须删除瓷砖前后的所有空行。
f.read().split()
更糟糕,现在,内存中有一个3Gb字符串和一个数组。。。如果您必须使用Python,正确的方法是@jDo,一次读取一行。您确定要将3GB加载到内存中吗?试着用jDo回答的方法+1对于拆分,如果文本被遗漏,它可能会破坏文本。因此,如果我想替换两个反斜杠,我必须放置三个反斜杠?如果波浪线的任意一侧有空格或null,我想删除整行。line.replace()方法是否也适用于这样的行?O.P.P.\~ O.P。