如何使用python脚本删除CSV文件中多行中常见的特定字符串?

时间:2015-07-06 06:49:47

标签: python regex csv

我有一个包含65000行(大小约为28 MB)的csv文件。在每一行中,给出了开头的某个路径,例如, "c:\abc\bcd\def\123\456"。现在让我们说路径"c:\abc\bcd\"在所有行中都很常见,其余内容也不同。我必须使用python脚本从所有行中删除公共部分(在本例中为"c:\abc\bcd\")。例如,CSV文件的内容如上所述。

C:/Abc/bcd/Def/Test/temp/test/GLNext/FILE0.frag                   0   0   0
C:/Abc/bcd/Def/Test/temp/test/GLNext/FILE0.vert                   0   0   0
C:/Abc/bcd/Def/Test/temp/test/GLNext/FILE0.link-link-0.frag       16  24  3
C:/Abc/bcd/Def/Test/temp/test/GLNext/FILE0.link-link-0.vert       87  116 69
C:/Abc/bcd/Def/Test/temp/test/GLNext/FILE0.link-link-0.vert.bin   75  95  61
C:/Abc/bcd/Def/Test/temp/test/GLNext/FILE0.link-link-0            0   0
C:/Abc/bcd/Def/Test/temp/test/GLNext/FILE0.link-link-6            0   0   0 

在上面的示例中,我需要输出如下

FILE0.frag                  0   0   0
FILE0.vert                  0   0   0
FILE0.link-link-0.frag      17  25  2
FILE0.link-link-0.vert      85  111 68
FILE0.link-link-0.vert.bin  77  97  60
FILE0.link-link-0               0   0
FILE0.link                  0   0   0

你们有没有人可以帮我解决这个问题?

6 个答案:

答案 0 :(得分:0)

怎么样,

import csv

with open("file.csv", 'rb') as f:
    sl = []
    csvread = csv.reader(f, delimiter=' ')
    for line in csvread:
        sl.append(line.replace("C:/Abc/Def/Test/temp\.\test\GLNext\", ""))

将列表sl写入filenew使用,

with open('filenew.csv', 'wb') as f:
    csvwrite = csv.writer(f, delimiter=' ')
    for line in sl:
        csvwrite.writerow(line)

答案 1 :(得分:0)

^\S+/

您可以在每一行上使用此正则表达式,并替换为empty string。请参阅演示。

https://regex101.com/r/cK4iV0/17

import re
p = re.compile(ur'^\S+/', re.MULTILINE)
test_str = u"C:/Abc/Def/Test/temp/test/GLNext/FILE0.frag                   0   0   0\nC:/Abc/Def/Test/temp/test/GLNext/FILE0.vert                   0   0   0\nC:/Abc/Def/Test/temp/test/GLNext/FILE0.link-link-0.frag       16  24  3\nC:/Abc/Def/Test/temp/test/GLNext/FILE0.link-link-0.vert       87  116 69\nC:/Abc/Def/Test/temp/test/GLNext/FILE0.link-link-0.vert.bin   75  95  61\nC:/Abc/Def/Test/temp/test/GLNext/FILE0.link-link-0            0   0\nC:/Abc/Def/Test/temp/test/GLNext/FILE0.link-link-6            0   0   0 "
subst = u" "

result = re.sub(p, subst, test_str)

答案 2 :(得分:0)

您可以自动检测公共前缀,而无需对其进行硬编码。你真的不需要regex。可以使用os.path.commonprefix 代替:

import csv
import os

with open('data.csv', 'rb') as csvfile:
    reader = csv.reader(csvfile)
    paths = [] #stores all paths
    rows = [] #stores all lines
    for row in reader:
        paths.append(row[0].split("/")) #split path by "/"
        rows.append(row)

    commonprefix = os.path.commonprefix(paths) #finds prefix common to all paths

    for row in rows:
        row[0] = row[0].replace('/'.join(commonprefix)+'/', "") #remove prefix

rows现在有一个可以写入文件的列表列表

with open('data2.csv', 'wb') as csvfile:
    writer = csv.writer(csvfile)
    for row in rows:
        writer.writerow(row)

答案 3 :(得分:0)

以下Python脚本将读取您的文件(假设它看起来像您的示例)并将创建一个删除公共文件夹的版本:

import os.path, csv

finput = open("d:\\input.csv","r")
csv_input = csv.reader(finput, delimiter=" ", skipinitialspace=True)
csv_output = csv.writer(open("d:\\output.csv", "wb"), delimiter=" ")

# Create a set of unique folder names

set_folders = set()

for input_row in csv_input:
    set_folders.add(os.path.split(input_row[0])[0])

# Determine the common prefix

base_folder = os.path.split(os.path.commonprefix(set_folders))[0]
nprefix = len(base_folder) + 1

# Go back to the start of the input CSV 

finput.seek(0)

for input_row in csv_input:
    csv_output.writerow([input_row[0][nprefix:]] + input_row[1:])

使用以下作为输入:

C:/Abc/Def/Test/temp/test/GLNext/FILE0.frag                   0   0   0
C:/Abc/Def/Test/temp/test/GLNext/FILE0.vert                   0   0   0
C:/Abc/Def/Test/temp/test/GLNext/FILE0.link-link-0.frag       16  24  3
C:/Abc/Def/Test/temp/test/GLNext2/FILE0.link-link-0.vert       87  116 69
C:/Abc/Def/Test/temp/test/GLNext5/FILE0.link-link-0.vert.bin   75  95  61
C:/Abc/Def/Test/temp/test/GLNext7/FILE0.link-link-0            0   0
C:/Abc/Def/Test/temp/test/GLNext/FILE0.link-link-6            0   0   0

输出如下:

GLNext/FILE0.frag 0 0 0
GLNext/FILE0.vert 0 0 0
GLNext/FILE0.link-link-0.frag 16 24 3
GLNext2/FILE0.link-link-0.vert 87 116 69
GLNext5/FILE0.link-link-0.vert.bin 75 95 61
GLNext7/FILE0.link-link-0 0 0
GLNext/FILE0.link-link-6 0 0 0

每列之间有一个空格,虽然这很容易改变。

答案 4 :(得分:0)

所以我试过这样的事情

for dirName, subdirList, fileList in os.walk(Directory):
    for fname in fileList:
        if fname.endswith('.csv'):
            for line in fileinput.input(os.path.join(dirName, fname), inplace = 1):
                location = line.find(r'GLNext')
                if location > 0:
                    location += len('GLNext')
                    print line.replace(line[:location], ".")
                else:
                    print line

答案 5 :(得分:0)

您可以使用pandas库进行此操作。这样做,您可以利用pandas'令人惊叹的处理大型CSV文件(即使是数百MB)。

<强>代码:

import pandas as pd

csv_file = 'test_csv.csv'
df = pd.read_csv(csv_file, header=None)
print df
print "-------------------------------------------"

path = "C:/Abc/bcd/Def/Test/temp/test/GLNext/"
df[0] = df[0].replace({path:""}, regex=True)

print df
# df.to_csv("truncated.csv") # Export to new file.

<强>结果:

                                                   0   1    2   3
0    C:/Abc/bcd/Def/Test/temp/test/GLNext/FILE0.frag   0    0   0
1    C:/Abc/bcd/Def/Test/temp/test/GLNext/FILE0.vert   0    0   0
2  C:/Abc/bcd/Def/Test/temp/test/GLNext/FILE0.lin...  16   24   3
3  C:/Abc/bcd/Def/Test/temp/test/GLNext/FILE0.lin...  87  116  69
4  C:/Abc/bcd/Def/Test/temp/test/GLNext/FILE0.lin...  75   95  61
5  C:/Abc/bcd/Def/Test/temp/test/GLNext/FILE0.lin...   0    0 NaN
6  C:/Abc/bcd/Def/Test/temp/test/GLNext/FILE0.lin...   0    0   0
-------------------------------------------
                            0   1    2   3
0                  FILE0.frag   0    0   0
1                  FILE0.vert   0    0   0
2      FILE0.link-link-0.frag  16   24   3
3      FILE0.link-link-0.vert  87  116  69
4  FILE0.link-link-0.vert.bin  75   95  61
5           FILE0.link-link-0   0    0 NaN
6           FILE0.link-link-6   0    0   0