CSV 파일을 가져 오는데 가격 중 하나에 0이 누락되었습니다.숫자를 달러 형식으로 변환하는 파이썬
원하는 출력 : 12.10
전류 출력을 : 12.1
어떻게 내가 영을 적용 할 것입니다 내 데이터에 포함되는? 내가 지금까지했던 어떤
:
#!/usr/bin/python
import csv
import sys
import argparse
#parsing command line options
parser = argparse.ArgumentParser(prog='desc', description=__doc__)
parser.add_argument('-i', '--input', help='Input file', nargs='?', type=argparse.FileType('r'), default=sys.stdin)
parser.add_argument('-o', '--output', help='Output file', nargs='?', type=argparse.FileType('w'), default=sys.stdout)
args = parser.parse_args(sys.argv[1:])
inf, outf = args.input, args.output
outf = csv.writer(outf)
print 'Loading %s file into memory' % inf.name
data = []
tmp = 0.00
for i, line in enumerate(csv.reader(inf)):
if i == 0:
outf.writerow(line)
continue
price = line[4]
price = price.replace('.', '')
# print price
if (price < 100):
tmp = price
tmp = tmp * 10
print tmp
소수점 이하 두 자리로 변수를 저장할 수 있습니까? 매번 libreoffice와 같은 CSV 프로그램에서 열면 마지막 0이 사라집니다. – chrisjlee
Excel 또는 LibreOffice로 CSV 생성 파일을 열면 파일을 해석하고 스프레드 시트에서 후미 0이 사라집니다. 뒤에 오는 0을 유지하려면 엑셀 파일이나 odf 문서를 직접 생성해야합니다 – Boud