简体   繁体   English

如何在csv文件python的所有行中删除逗号

[英]How to remove commas in all lines of csv file python

I know that there are some topics on this that tell us to use .strip() or .rstrip() function to do this but it's not working for me. 我知道有一些主题告诉我们使用.strip()或.rstrip()函数来执行此操作,但这对我不起作用。

I have a programme that appends a new line to the csv file but unfortunately it generates a trailing comma... I have tried to remove it with the .strip() function in python but it isn't working well, I am doing something wrong? 我有一个程序将一个新行添加到csv文件中,但不幸的是它会产生一个尾随的逗号...我试图用python中的.strip()函数将其删除,但效果不佳,我正在做一些事情错误? This is an example of what happen when I input 'T123' for Bike_No and '05/08/2017' for Purchase_Date 这是当我为Bike_No输入“ T123”并为Purchase_Date输入“ 05/08/2017”时发生的情况的示例

 from datetime import datetime
    td= datetime.now()
    initial_bike_detaillist=[]
    deBatt =100
    deKM = 0.00
    deDate = str(td)[8:10] + "/"+ str(td)[5:7] + "/"+ str(td)[0:4]
    print("Option 4: Add abicycle \n")
    Bike_No=input("Bike No. :")
    Purchase_Date=str(input("Purchase Date:"))
    initial_bike_detaillist=[str(Bike_No),str(Purchase_Date),str(deBatt),str(deDate),str(deKM)]#because there  is no write function for int
    filename="Assignment_Data1.csv"
    file=open(filepath + filename,"a")
    file.write("\n")
    for k in initial_bike_detaillist:
        file.write("{},".format(k))
    print("Bicycle ({}) has been created".format(Bike_No))
    file.close()
    file=open(filepath + filename,"r")
    for line in file:
        line.strip()
        print(line)

expected output= 预期输出=

Bike No.,Purchase Date,Batt %,Last Maintenance,KM since Last 

T101,10/04/2016,55,10/01/2017,25.08

T102,01/07/2016,10,15/05/2017,30.94

T103,15/11/2016,94,13/06/2017,83.16

T104,25/04/2017,58,10/01/2017,25.08

T105,24/05/2017,5,20/06/2017,93.80

T123,04/04/2017,100,05/08/2017,0.0

actual output: 实际输出:

Bike No.,Purchase Date,Batt %,Last Maintenance,KM since Last 

T101,10/04/2016,55,10/01/2017,25.08

T102,01/07/2016,10,15/05/2017,30.94

T103,15/11/2016,94,13/06/2017,83.16

T104,25/04/2017,58,10/01/2017,25.08

T105,24/05/2017,5,20/06/2017,93.80

T123,04/04/2017,100,05/08/2017,0.0,

` `

Instead of this line : 代替这一行:

for k in initial_bike_detaillist:
    file.write("{},".format(k))

use following line : 使用以下行:

file.write(','.join(initial_bike_detaillist))

Your Code : 您的代码:

from datetime import datetime

td = datetime.now()
initial_bike_detaillist = []
deBatt = 100
deKM = 0.00
deDate = str(td)[8:10] + "/" + str(td)[5:7] + "/" + str(td)[0:4]
print("Option 4: Add abicycle \n")
Bike_No = input("Bike No. :")
Purchase_Date = str(input("Purchase Date:"))
initial_bike_detaillist = [str(Bike_No), str(Purchase_Date), str(deBatt), str(deDate),
                           str(deKM)]  # because there  is no write function for int
filename = "Assignment_Data1.csv"
file = open(filepath + filename, "a")
file.write("\n")
# for k in initial_bike_detaillist:
#   file.write("{},".format(k))
file.write(','.join(initial_bike_detaillist)) # use this line .

print("Bicycle ({}) has been created".format(Bike_No))
file.close()
file = open(filepath + filename, "r")
for line in file:
  # line.strip() # Then, not need this line
  print(line)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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