import csv
with open('test.csv', 'r+', newline='') as f: # open file
lines = list(csv.reader(f, delimiter='|')) # reads file in as a list of lists
for i, line in enumerate(lines):
if i > 0: # skips the header
lines[i][0] = line[0].replace('-', '') # replaces the -
f.seek(0) # locates the beginning of the file
f.truncate() # removes previous contents
writer = csv.writer(f)
writer.writerows(lines) # write back to csv file
# resulting file
Pat_ID,System_Disc,Syndrom_Disk
UP009,Respiratory,Pneumonia
UP010,Skin,Abscess
使用熊猫
,
之外的分隔符,请使用sep
参数使用CSV
相关问题 更多 >
编程相关推荐