Python 如果存在某个值,则删除CSV文件中的单元格
我有两种类型的CSV文件,我想将它们合并在一起。 要做到这一点,我想在每一行中找到某些值,如果它们存在,就将它们删除 我尝试使用list.Index或list.Remove函数,但当值不在特定文件中时,我遇到了一个错误 例如,这两行是(为了更好地显示,我剪切了其余的行): 我想找到值为“3270”和“2500”的单元格,以便两个文件对齐。。。 之后,我想删除空单元格,以便再次-它们将对齐 你能帮我弄明白怎么做吗 谢谢,Python 如果存在某个值,则删除CSV文件中的单元格,python,list,parsing,csv,indexing,Python,List,Parsing,Csv,Indexing,我有两种类型的CSV文件,我想将它们合并在一起。 要做到这一点,我想在每一行中找到某些值,如果它们存在,就将它们删除 我尝试使用list.Index或list.Remove函数,但当值不在特定文件中时,我遇到了一个错误 例如,这两行是(为了更好地显示,我剪切了其余的行): 我想找到值为“3270”和“2500”的单元格,以便两个文件对齐。。。 之后,我想删除空单元格,以便再次-它们将对齐 你能帮我弄明白怎么做吗 谢谢, 尼姆罗德。很难说清楚你希望完成什么,但我认为这应该让你开始 #!/usr/b
尼姆罗德。很难说清楚你希望完成什么,但我认为这应该让你开始
#!/usr/bin/env python
import csv
myfile = '/path/to/untitled.csv'
newfile = '/path/to/untitled_new.csv'
reader = csv.reader(open(myfile))
remove_me = {'3270','2500'}
print('Before:')
print(open(myfile).read())
for row in reader:
new_row = []
for column in row:
if column not in remove_me:
new_row.append(column)
csv.writer(open(newfile,'a')).writerow(new_row)
print('\n\n')
print('After:')
print(open(newfile).read())
输出:
Before:
1950,1300,1180,48,48,400
3270,2500,1950,1300,1180
After:
1950,1300,1180,48,48,400
1950,1300,1180
在迭代同一个列表时,确保没有使用list.remove,否则可能会把自己搞砸。上面我的代码使用了一种更安全的策略,即将通过if
测试的值(列是而不是您想要删除的值)复制到一个新列表中,然后将该新列表写入一个新的.csv文件
这或多或少是你打算做的吗
为了去除空白单元格,我想你可以添加
'
到删除
很难确切地说出你希望实现什么,但我认为这应该让你开始
#!/usr/bin/env python
import csv
myfile = '/path/to/untitled.csv'
newfile = '/path/to/untitled_new.csv'
reader = csv.reader(open(myfile))
remove_me = {'3270','2500'}
print('Before:')
print(open(myfile).read())
for row in reader:
new_row = []
for column in row:
if column not in remove_me:
new_row.append(column)
csv.writer(open(newfile,'a')).writerow(new_row)
print('\n\n')
print('After:')
print(open(newfile).read())
输出:
Before:
1950,1300,1180,48,48,400
3270,2500,1950,1300,1180
After:
1950,1300,1180,48,48,400
1950,1300,1180
在迭代同一个列表时,确保没有使用list.remove,否则可能会把自己搞砸。上面我的代码使用了一种更安全的策略,即将通过if
测试的值(列是而不是您想要删除的值)复制到一个新列表中,然后将该新列表写入一个新的.csv文件
这或多或少是你打算做的吗
为了去除空白单元格,我假设您可以将
'
添加到删除
我建议您循环文件中的每个值,然后设置一些条件删除元素,然后将这些值合并到新的输出文件中
步骤1读取文件
import sys
import csv
updatedlist = []
for val in csv.reader(open('input1.csv' , 'rb')) :
print val
## val will be a list of each row ,
## So in this case you will have to
## select the elements while you will be looping
## then replace or removing the elements you want to remove
## and make a new updated list which you will then have
## to append to a new output.csv file
## then do the same the to the other file and append to the output.csv file
for Values in updatedlist :
##looping thru the values and then make a string of the values separated by commas
f = open('output.csv' , 'a')
f.writelines(updatestring) ##updated list with a string of comma separated values
f.close()
我建议您循环文件中的每个值,然后设置一些条件删除元素,然后将这些值合并到新的输出文件中 步骤1读取文件
import sys
import csv
updatedlist = []
for val in csv.reader(open('input1.csv' , 'rb')) :
print val
## val will be a list of each row ,
## So in this case you will have to
## select the elements while you will be looping
## then replace or removing the elements you want to remove
## and make a new updated list which you will then have
## to append to a new output.csv file
## then do the same the to the other file and append to the output.csv file
for Values in updatedlist :
##looping thru the values and then make a string of the values separated by commas
f = open('output.csv' , 'a')
f.writelines(updatestring) ##updated list with a string of comma separated values
f.close()
你是如何排列这些文件的?您是否正在使用前两个
3270
和2500
创建单元格,并删除之前的所有内容?它们是否总是3270
和2500
?CSV文件顶部是否有一行包含列名?文件中的前几行可能不相同。。。数据行是相同的,除了一种类型的文件有两个单元格,而另一种没有。-例如,3270和其他值位于1种类型的文件中,而不位于其他类型的文件中。您能否提供更广泛的数据示例?另外,第1行不是缺少值,而是缺少第2行;比如,是什么决定了任何给定的行应该被“拉”到左边或右边?迪伯,我不确定是什么混淆。。。我所要做的就是遍历一行,如果它的值为“3270”-将其删除。这在python中是不可能的吗?如何对齐文件?您是否正在使用前两个3270
和2500
创建单元格,并删除之前的所有内容?它们是否总是3270
和2500
?CSV文件顶部是否有一行包含列名?文件中的前几行可能不相同。。。数据行是相同的,除了一种类型的文件有两个单元格,而另一种没有。-例如,3270和其他值位于1种类型的文件中,而不位于其他类型的文件中。您能否提供更广泛的数据示例?另外,第1行不是缺少值,而是缺少第2行;比如,是什么决定了任何给定的行应该被“拉”到左边或右边?迪伯,我不确定是什么混淆。。。我所要做的就是遍历一行,如果它的值为“3270”-将其删除。这在python中不可能吗?我喜欢你的方式。为了解决这个问题,我做了如下操作:如果NewRow[1]='3270':del NewRow[1:3]del NewRow[4:9]del NewRow[6]
我喜欢你的方式。为了解决这个问题,我做了如下操作:if NewRow[1]='3270':del NewRow[1:3]del NewRow[4:9]del NewRow[6]