我必須在我的csv文件中爲特定單元格寫入一個值(比如第8個單元格)。 我可以看到有一個csvwriter.writerow(row)
方法來寫整行,但我沒有看到任何東西寫入一個特定的單元格的值。在Python中使用csv模塊寫入特定單元格
6
A
回答
8
csv module提供了讀寫csv文件的工具,但不允許修改特定的單元格就地。
即使您在問題中突出顯示的csvwriter.writerow(row)
方法也不允許您識別並覆蓋特定的行。而是將row
參數寫入作者的文件對象,實際上它只是將與作者相關聯的csv文件附加到一行。
不要被勸阻使用csv module雖然它使用起來很簡單,並且提供的原語可以實現您比較容易找到的更高級別的功能。
例如看看下面的CSV文件:
1,2,3,four,5
1,2,3,four,5
1,2,3,four,5
字four
是第3列(第四列,但一排就是這麼的索引是從零開始的列表),這樣可以能夠容易地更新爲包含數字4
與以下程序:
import csv
in_file = open("d:/in.csv", "rb")
reader = csv.reader(in_file)
out_file = open("d:/out.csv", "wb")
writer = csv.writer(out_file)
for row in reader:
row[3] = 4
writer.writerow(row)
in_file.close()
out_file.close()
在輸出所得:
1,2,3,4,5
1,2,3,4,5
1,2,3,4,5
授予創建一些通用函數,允許識別和更新特定的行和列是多一點工作,但沒有太多更多,因爲在Python中操作csv文件只是操縱一系列列表。
1
我同意,這很煩人。我完成了csv.DictReader的子類化。這允許基於單元格的查找編輯和轉儲。我已經張貼在ActiveState公司代碼:In place csv lookup, manipulation and export
import csv, collections, copy
"""
# CSV TEST FILE 'test.csv'
TBLID,DATETIME,VAL
C1,01:01:2011:00:01:23,5
C2,01:01:2012:00:01:23,8
C3,01:01:2013:00:01:23,4
C4,01:01:2011:01:01:23,9
C5,01:01:2011:02:01:23,1
C6,01:01:2011:03:01:23,5
C7,01:01:2011:00:01:23,6
C8,01:01:2011:00:21:23,8
C9,01:01:2011:12:01:23,1
#usage (saving this cose as CustomDictReader.py)
>>> import CustomDictReader
>>> import pprint
>>> test = CustomDictReader.CSVRW()
>>> success, thedict = test.createCsvDict('TBLID',',',None,'test.csv')
>>> pprint.pprint(dict(thedict))
{'C1': OrderedDict([('TBLID', 'C1'), ('DATETIME', '01:01:2011:00:01:23'), ('VAL', '5')]),
'C2': OrderedDict([('TBLID', 'C2'), ('DATETIME', '01:01:2012:00:01:23'), ('VAL', '8')]),
'C3': OrderedDict([('TBLID', 'C3'), ('DATETIME', '01:01:2013:00:01:23'), ('VAL', '4')]),
'C4': OrderedDict([('TBLID', 'C4'), ('DATETIME', '01:01:2011:01:01:23'), ('VAL', '9')]),
'C5': OrderedDict([('TBLID', 'C5'), ('DATETIME', '01:01:2011:02:01:23'), ('VAL', '1')]),
'C6': OrderedDict([('TBLID', 'C6'), ('DATETIME', '01:01:2011:03:01:23'), ('VAL', '5')]),
'C7': OrderedDict([('TBLID', 'C7'), ('DATETIME', '01:01:2011:00:01:23'), ('VAL', '6')]),
'C8': OrderedDict([('TBLID', 'C8'), ('DATETIME', '01:01:2011:00:21:23'), ('VAL', '8')]),
'C9': OrderedDict([('TBLID', 'C9'), ('DATETIME', '01:01:2011:12:01:23'), ('VAL', '1')])}
>>> thedict.keys()
['C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9']
>>> thedict['C2']['VAL'] = "BOB"
>>> pprint.pprint(dict(thedict))
{'C1': OrderedDict([('TBLID', 'C1'), ('DATETIME', '01:01:2011:00:01:23'), ('VAL', '5')]),
'C2': OrderedDict([('TBLID', 'C2'), ('DATETIME', '01:01:2012:00:01:23'), ('VAL', 'BOB')]),
'C3': OrderedDict([('TBLID', 'C3'), ('DATETIME', '01:01:2013:00:01:23'), ('VAL', '4')]),
'C4': OrderedDict([('TBLID', 'C4'), ('DATETIME', '01:01:2011:01:01:23'), ('VAL', '9')]),
'C5': OrderedDict([('TBLID', 'C5'), ('DATETIME', '01:01:2011:02:01:23'), ('VAL', '1')]),
'C6': OrderedDict([('TBLID', 'C6'), ('DATETIME', '01:01:2011:03:01:23'), ('VAL', '5')]),
'C7': OrderedDict([('TBLID', 'C7'), ('DATETIME', '01:01:2011:00:01:23'), ('VAL', '6')]),
'C8': OrderedDict([('TBLID', 'C8'), ('DATETIME', '01:01:2011:00:21:23'), ('VAL', '8')]),
'C9': OrderedDict([('TBLID', 'C9'), ('DATETIME', '01:01:2011:12:01:23'), ('VAL', '1')])}
>>> test.updateCsvDict(thedict)
>>> test.createCsv('wb')
"""
class CustomDictReader(csv.DictReader):
"""
override the next() function and use an
ordered dict in order to preserve writing back
into the file
"""
def __init__(self, f, fieldnames = None, restkey = None, restval = None, dialect ="excel", *args, **kwds):
csv.DictReader.__init__(self, f, fieldnames = None, restkey = None, restval = None, dialect = "excel", *args, **kwds)
def next(self):
if self.line_num == 0:
# Used only for its side effect.
self.fieldnames
row = self.reader.next()
self.line_num = self.reader.line_num
# unlike the basic reader, we prefer not to return blanks,
# because we will typically wind up with a dict full of None
# values
while row == []:
row = self.reader.next()
d = collections.OrderedDict(zip(self.fieldnames, row))
lf = len(self.fieldnames)
lr = len(row)
if lf < lr:
d[self.restkey] = row[lf:]
elif lf > lr:
for key in self.fieldnames[lr:]:
d[key] = self.restval
return d
class CSVRW(object):
def __init__(self):
self.file_name = ""
self.csv_delim = ""
self.csv_dict = collections.OrderedDict()
def setCsvFileName(self, name):
"""
@brief stores csv file name
@param name- the file name
"""
self.file_name = name
def getCsvFileName(self):
"""
@brief getter
@return returns the file name
"""
return self.file_name
def getCsvDict(self):
"""
@brief getter
@return returns a deep copy of the csv as a dictionary
"""
return copy.deepcopy(self.csv_dict)
def clearCsvDict(self):
"""
@brief resets the dictionary
"""
self.csv_dict = collections.OrderedDict()
def updateCsvDict(self, newCsvDict):
"""
creates a deep copy of the dict passed in and
sets it to the member one
"""
self.csv_dict = copy.deepcopy(newCsvDict)
def createCsvDict(self,dictKey, delim, handle = None, name = None, readMode = 'rb', **kwargs):
"""
@brief create a dict from a csv file where:
the top level keys are the first line in the dict, overrideable w/ **kwargs
each row is a dict
each row can be accessed by the value stored in the column associated w/ dictKey
that is to say, if you want to index into your csv file based on the contents of the
third column, pass the name of that col in as 'dictKey'
@param dictKey - row key whose value will act as an index
@param delim - csv file deliminator
@param handle - file handle (leave as None if you wish to pass in a file name)
@param name - file name (leave as None if you wish to pass in a file handle)
@param readMode - 'r' || 'rb'
@param **kwargs - additional args allowed by the csv module
@return bool - SUCCESS|FAIL
"""
self.csv_delim = delim
try:
if isinstance(handle, file):
self.setCsvFileName(handle.name)
reader = CustomDictReader(handle, delim, **kwargs)
else:
if None == name:
name = self.getCsvFileName()
else:
self.setCsvFileName(name)
reader = CustomDictReader(open(name, readMode), delim, **kwargs)
for row in reader:
self.csv_dict[row[dictKey]] = row
return True, self.getCsvDict()
except IOError:
return False, 'Error opening file'
def createCsv(self, writeMode, outFileName = None, delim = None):
"""
@brief create a csv from self.csv_dict
@param writeMode - 'w' || 'wb'
@param outFileName - file name || file handle
@param delim - csv deliminator
@return none
"""
if None == outFileName:
outFileName = self.file_name
if None == delim:
delim = self.csv_delim
with open(outFileName, writeMode) as fout:
for key in self.csv_dict.values():
fout.write(delim.join(key.keys()) + '\n')
break
for key in self.csv_dict.values():
fout.write(delim.join(key.values()) + '\n')
0
假設你有一個名爲mylist.csv csv文件具有以下行:
a, b, c, d
e, f, g, h
i, j, k, l
,如果你想修改「H」,成爲「X」 ,可以使用此代碼,需要將導入CSV模塊:
f = open('mylist.csv', 'r')
reader = csv.reader(f)
mylist = list(reader)
f.close()
mylist[1][3] = 'X'
my_new_list = open('mylist.csv', 'w', newline = '')
csv_writer = csv.writer(my_new_list)
csv_writer.writerows(mylist)
my_new_list.close()
如果要修改於各行的特定列,只需添加for循環迭代。
相關問題
- 1. 用python插入csv中的特定單元格的值
- 2. 寫入csv文件中的特定單元格
- 3. python在一個單元格中寫入csv
- 4. 如何在csv中使用Python將字符串寫入一個單元格?
- 5. 使用python將數據表單特定的csv單元格寫入另一個文件
- 6. C#從CSV文件中寫出特定的單元格
- 7. python csv寫入器將源代碼片段寫入單個單元格
- 8. 當使用python csv模塊
- 9. python CSV模塊 - 只獲取一個單元格填充
- 10. 使用Python的CSV模塊覆蓋csv文件中的特定行
- 11. 在單個單元格中寫入多個值csv
- 12. python CSV寫入器 - 格式
- 13. 使用csv模塊重寫後的Python - CSV文件爲空
- 14. 基於CSV單元格寫入XML文件名Python
- 15. 將熊貓數據框值寫入特定表格中的特定單元格。
- 16. 使用python寫入特定列是.csv文件
- 17. 在Python中使用CSV模塊替換其他值使用Python
- 18. Python - 用於將某些行寫入新文件的CSV模塊
- 19. 如何使用DataReader在excel單元格中編寫如果特定單元格是使用C#的「ReadOnly」
- 20. Python XLWT:將列表寫入單元格
- 21. python寫入EXCEL單元格範圍(NumPy?)
- 22. Python的CSV模塊同時讀寫
- 23. 從C++在Excel中寫入單元格 - 沒有寫入值,單元格爲空
- 24. 用逗號和雙引號在Python中的單元格中編寫csv文件
- 25. Drupal 6:如何在使用表格模塊時在特定單元格上使用form_set_error
- 26. 在python中使用模塊netcdf4寫入NetCDF3文件
- 27. 使用RPostgreSQL寫入特定模式
- 28. 在Python中導入和使用模塊
- 29. python將標題寫入csv,一個單元格中的所有標題
- 30. Python:刮特殊字符寫入CSV
'def createCSV(writemode' should become'def createCSV(self,....)' – pyInTheSky 2016-09-16 13:56:02
糾正後test.createCsv('wb')不會寫入我加載的csv的任何更改?Ideas? – 2016-09-16 18:53:48
我猜你忘了'test.updateCsvDict(thedict)'......我已經完全更新了代碼和樣例用法,希望它有幫助,雖然這個類沒有互斥鎖保護,但我還是寫了一個意圖它可以鎖定getter/setter/writer ...所以createCsv函數只會寫出類的csv副本,這是處女讀,除非你先調用update。 – pyInTheSky 2016-09-18 03:59:22