从CSV列中删除1000的分隔符?

时间:2018-10-31 22:54:57

标签: python csv

我有一个Python脚本,其中要导入一个逗号分隔的csv,其值超过1000。这些值是csv中的字符串。我需要从值中删除逗号,然后将其转换为csv中的圆形浮点数,然后再将其导入Python。

我尝试将所有新值附加到列表中以使用csv.writer,但是我还没弄清楚如何让编写者仅替换具有逗号的列中的值。到目前为止,这就是我所拥有的。 :

import csv

RoomReport = r'path_to_csv'
new_values_list = []

f = open(RoomReport, "r")
reader = csv.reader(f)
writer = csv.writer(f)

for row in reader:
     useable_area = row[7]

     if "," in useable_area:
         useable_area_no_comma = useable_area.replace(",","")
         useable_area_rounded = int(round(float(useable_area_no_comma)))
         new_values_list.append(useable_area_rounded)


f.close()

2 个答案:

答案 0 :(得分:0)

也许是这样吗?

import re
from sys import stdout

isnum = re.compile('^[0-9, ]+$')
non = re.compile('[, ]')

fd = StringIO()
out = csv.writer(fd)
out.writerow(['foo','1,000,000',19])
out.writerow(['bar','1,234,567',20])

fd.seek(0)
inp = csv.reader(fd)
out = csv.writer(stdout)
for row in inp:
    for i, x in enumerate(row):
        if isnum.match(x):
            row[i] = float(non.sub('', x))
    out.writerow(row)

答案 1 :(得分:0)

正如我在评论中提到的那样,只有将输入的csv文件格式化后,数字中的逗号才能与其中的每个逗号区分。他们。

这是一种完成方法的示例(通过引用所有值):

"0","1","2","3","4","5","6","7,123.6","8","9"
"0","1","2","3","4","5","6","1,000","8","9"
"0","1","2","3","4","5","6","20,000","8","9"

这里的代码可以满足您的需求。它使用locale.atof函数来简化数字清除工作:

import csv
import locale

# Set local to someplace that uses a comma for the thousands separator.
locale.setlocale(locale.LC_ALL, 'English_US.1252')

RoomReport = r'RoomReport.csv'
cleaned_report = r'RoomReport_cleaned.csv'
new_values_list = []

with open(RoomReport, "r", newline='') as inp:
    for row in csv.reader(inp):
        if "," in row[7]:
            row[7] = int(round(locale.atof(row[7])))

        new_values_list.append(row)

# Create cleaned-up output file.
with open(cleaned_report, "w", newline='') as outp:
    csv.writer(outp, quoting=csv.QUOTE_ALL).writerows(new_values_list)

它从示例输入中创建的RoomReport_cleaned.csv将包含以下内容:

"0","1","2","3","4","5","6","7124","8","9"
"0","1","2","3","4","5","6","1000","8","9"
"0","1","2","3","4","5","6","20000","8","9"

请注意,由于输出中的值不再嵌入逗号,因此不再需要引用所有字段,因此可以通过 not 指定csv.QUOTE_ALL来忽略。 / p>