Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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_Csv - Fatal编程技术网

Python 删除字符的所有尾随引用

Python 删除字符的所有尾随引用,python,csv,Python,Csv,我有一个CSV需要清理。它的记录大致如下: 1, a[]b[][][], c[]d[][][] 2, a[]b[]c[]d[]e, a[]b[]c[]d[]e 字段中出现的任何拖尾[]都需要从行中删除,然后将这些行写入新文件。例如,在这种情况下,第1行将变为1,a[]b,c[]d,第2行将保持不变 以下是我所得到的信息: import csv new_rows = [] with open('input.csv', 'rb') as f: reader = csv.reader(f

我有一个CSV需要清理。它的记录大致如下:

1, a[]b[][][], c[]d[][][]
2, a[]b[]c[]d[]e, a[]b[]c[]d[]e
字段中出现的任何拖尾
[]
都需要从行中删除,然后将这些行写入新文件。例如,在这种情况下,第1行将变为
1,a[]b,c[]d
,第2行将保持不变

以下是我所得到的信息:

import csv

new_rows = []

with open('input.csv', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:    
        new_row = [i.split(',').rstrip('[]') for i in row] #won't work since this is a list, not a string
        new_rows.append(new_row) 

with open('output.csv', 'wb') as f:
    writer = csv.writer(f)
    writer.writerows(new_rows)

我知道rstrip只对字符串有效,而不是列表。我对Python有点生疏,不太懂,有人能帮我一下吗?

差不多了,看起来只是一个错误的拆分

import csv

new_rows = []

with open('input.csv', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:    
        new_row = [x.rstrip('[]') for x in row]
        new_rows.append(new_row) 

with open('output.csv', 'wb') as f:
    writer = csv.writer(f)
    writer.writerows(new_rows)
根据以下信息输入:

1, a[]b[][][], c[]d[][][]
2, a[]b[]c[]d[]e, a[]b[]c[]d[]e
我得到以下信息:

1, a[]b, c[]d
2, a[]b[]c[]d[]e, a[]b[]c[]d[]e

就快到了,看起来只是一个错误的分离

import csv

new_rows = []

with open('input.csv', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:    
        new_row = [x.rstrip('[]') for x in row]
        new_rows.append(new_row) 

with open('output.csv', 'wb') as f:
    writer = csv.writer(f)
    writer.writerows(new_rows)
根据以下信息输入:

1, a[]b[][][], c[]d[][][]
2, a[]b[]c[]d[]e, a[]b[]c[]d[]e
我得到以下信息:

1, a[]b, c[]d
2, a[]b[]c[]d[]e, a[]b[]c[]d[]e
在Python2.7之前,可以将上下文管理器组合成一个
with
语句

import contextlib
with contextlib.nested(open('input.csv', 'rb'), open('output.csv', 'wb')) as (inf, outf):
    reader = ...
在Python 2.7中,对
with
语句本身进行了修改,以支持以下内容:

with open('input.csv', 'rb') as inf, open('output.csv', 'wb') as outf:
虽然隐式直线延续不可用;要跨越两条线,您必须 使用行连续字符:

with open('input.csv', 'rb') as inf,\
     open('output.csv', 'rb') as outf:
在Python2.7之前,可以将上下文管理器组合成一个
with
语句

import contextlib
with contextlib.nested(open('input.csv', 'rb'), open('output.csv', 'wb')) as (inf, outf):
    reader = ...
在Python 2.7中,对
with
语句本身进行了修改,以支持以下内容:

with open('input.csv', 'rb') as inf, open('output.csv', 'wb') as outf:
虽然隐式直线延续不可用;要跨越两条线,您必须 使用行连续字符:

with open('input.csv', 'rb') as inf,\
     open('output.csv', 'rb') as outf:

您在
|
上拆分有什么原因吗?我在输入中没有看到它。您可以在
split
之前尝试
rstrip
。很好,谢谢。我在CSV之间来回跳跃,但忽略了这一点。根据@chepner的回答,没有必要将结果保存到中间的
新行
列表中。边走边写。读起来更简单,内存效率也更高。您在
|
上拆分有什么原因吗?我在输入中没有看到它。您可以在
split
之前尝试
rstrip
。很好,谢谢。我在CSV之间来回跳跃,但忽略了这一点。根据@chepner的回答,没有必要将结果保存到中间的
新行
列表中。边走边写。它的阅读更简单,存储效率更高。非常好,谢谢!经常是这样简单的事情。太好了,谢谢!通常是这样简单。请注意,如果您不喜欢嵌套的
with
语句导致的多个缩进,您可以将它们粉碎在一行
上,使用open('input.csv','rb')作为inf,open('output.csv','wb')作为outp:
。还要注意的是,通过使用行延续符(````),可以将拼凑在一起的行放在两行上(我更喜欢这两行都有单独的行,但只有一个缩进)在Python 2.7之前,不支持使用单个
with
语句的多个上下文管理器。请注意,如果您不喜欢嵌套的
with
语句导致的多个缩进,可以使用open('input.csv','rb')作为inf,open('output.csv','wb')作为outp:将它们粉碎在一行
上。还要注意的是,通过使用行延续(````),可以将拼凑在一起的行放在两行上(我认为最好是两行都有单独的行,但只有一个缩进)。在Python 2.7之前,不支持使用单个
with
语句的多个上下文管理器。