0
我有數據如CSV:如何打印字符串分割的第一部分在python
Minneapolis-St. Paul-Bloomington MN-WI--33460 Charlotte-Gastonia-Rock Hill NC-SC--16740 Las Vegas-Paradise NV--29820 New York-White Plains-Wayne NY-NJ (MSAD)--35620
我們怎樣才能之前只打印數據 - 在列?
i.e only : Minneapolis-St. Paul-Bloomington MN-WI Charlotte-Gastonia-Rock Hill NC-SC Las Vegas-Paradise NV & so on....
代碼:
#!/usr/bin/python
import csv
import re
sample="""columnA,ColumnB,columnC
1,Cleveland-Elyria-Mentor OH--17460
2,Dallas-Plano-Irving TX (MSAD)--19100
3,ASJDFJKDJ-kD-JE WA--21092"""
open('sample.csv','w').write(sample)
with open('sample.csv') as sample, open('final_output.csv','w') as output:
reader = csv.reader(sample)
writer = csv.writer(output)
# discard input header
next(reader)
# write output header
writer.writerow(['col1','col2','col3'])
#process rows
for row in reader:
if row:
for fipsplit in row[1].split('\n'):
fipsval = re.findall(r'--(\d+)$', fipsplit)
writer.writerow([fipsplit])
print open('final_output.csv').read()
注後打印第一個元素:組功能不python2.7 CSV作家 – Viv
爲什麼工作不只是'row [: - 7]',如果行總是有7個字符在你想要刪除的末尾。 –