简体   繁体   中英

How to remove all dots from a csv file?

Can someone explain line 7? I know that in the end, all commas are replaced by dots, but how does that work? And what should I do if I want to remove all the dots from the CSV file (without pandas)?

def collector(request):
    with open("C:\\Users\\Simon\\Dev\\Water\\src\\csv\\items_water.csv") as csv_file:
        reader = csv.reader(csv_file)
        for row in reader:
            if row == [] or row[1] == "products":
                continue
            price = float(str(row[1].split(",")[0]) + "." + row[1].split(",")[1])
            print(price)

The important lines :

row[1]           # take the second element of row,
  .split(",")[0] # split it on commas and takes the first part of it
                 # "123,456" => "123"

  .split(",")[1] # Does the same thing but with the second part of it

With this, if row[1] was "123,456" we would get "123" + "." + "456" "123" + "." + "456"

This is then cast into a string(most likely useless) then into a float

A much simpler way to accomplish this would be price = row[1].replace(',', '.', 1)

To remove all the dots from a string, simply use str.replace like above but by ommiting the last paramter count.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM