[英]how to delete the annoying data in the csv file
我想在我的csv文件中刪除一些字符串(“ Description”“ This is a Simulation”),並且我也想刪除數據中的一些“ =”和數據末尾的“,”。 該文件如下所示
"time","student","items"
="09:00:00","Tim","apple",
="09:00:10","Jason","orange",
"09:10:10","Emily","grape",
"09:22:10","Ivy","kiwi",
"Description"
"This is a simulation"
我嘗試過.pop()。 它沒有用
ff= []
import csv
with open('file.csv') as f:
for row in csv.DictReader(f):
row.replace(',','')
ff.append(row)
我想得到這樣的:
"time","student","items"
"09:00:00","Tim","apple"
"09:00:10","Jason","orange"
"09:10:10","Emily","grape"
"09:22:10","Ivy","kiwi"
您可能希望將文件讀取為原始文本文件而不是csv,以便您更輕松地執行字符串操作。
編輯:我假設tmp
是CSV文件的路徑,而<list data>
是由csv.DictReader
生成的字典列表。 然后,您可以通過執行2個主要步驟來編寫convert(tmp)
。 一種是將文件重新格式化為臨時文件,另一種是使用csv.DictReader
將臨時文件讀入字典數據列表。 讀取完數據后,將使用os
模塊刪除臨時文件:
import csv
import os
def convert(tmp):
new_lines = []
temp_file = tmp + '.tmp'
with open(tmp) as fd:
for line in fd:
# remove new line characters
line = line.replace('\n', '').replace('\r', '')
# delete string
line = line.replace('=', '').replace('"Description"', '').replace('"This is a simulation"', '')
# don't add empty string
if line.strip() == '':
continue
# remove last line commas
if line[-1] == ',':
line = line[:-1]
new_lines.append(line)
# write formatted data to temporary csv file
with open(temp_file, 'w') as fd:
fd.write('\n'.join(new_lines))
# get list data
ff = None
with open(temp_file) as f:
ff = list(csv.DictReader(f))
# delete temporary file
os.remove(temp_file)
return ff
print convert('./file.csv')
大多數情況下利用內置的str
方法,並假設第一行始終是有效的標題行。
ff = []
with open('file.csv') as f:
for row in f:
# strip empty lines, and head/tail = ,
line = row.strip().strip('=').strip(',')
# skip empty lines
if not line:
continue
# assume first row is always a valid header row
# split by comma to see if it matches header row
if not len(ff) or (len(line.split(',')) == len(ff[0].split(','))):
ff.append(line)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.