如何使用Python删除CSV中每行的末尾的逗号?

时间:2017-02-11 02:59:17

标签: python csv pandas numpy comma

我正在玩比特币数据,想要删除CSV文件中每行末尾的逗号。例如,CSV数据看起来像这样....

Date Time,Open,High,Low,Close,Volume
2012-01-01 04:15:00,4.58,4.58,4.58,4.58,1.502,
2012-01-01 15:15:00,4.84,4.84,4.84,4.84,10.0,
2012-01-01 22:45:00,5.0,5.0,5.0,5.0,10.1,
2012-01-02 20:00:00,5.0,5.0,5.0,5.0,19.048,
2012-01-03 11:45:00,5.32,5.32,5.32,5.32,2.41917293,
2012-01-03 14:15:00,5.14,5.14,5.14,5.14,0.68,

我想让它看起来像这样

Date Time,Open,High,Low,Close,Volume
2012-01-01 04:15:00,4.58,4.58,4.58,4.58,1.502
2012-01-01 15:15:00,4.84,4.84,4.84,4.84,10.0
2012-01-01 22:45:00,5.0,5.0,5.0,5.0,10.1
2012-01-02 20:00:00,5.0,5.0,5.0,5.0,19.048
2012-01-03 11:45:00,5.32,5.32,5.32,5.32,2.41917293
2012-01-03 14:15:00,5.14,5.14,5.14,5.14,0.68

我应该使用Pandas或Numpy来实现我想要的吗?如果是这样,我该怎么做?谢谢!

4 个答案:

答案 0 :(得分:1)

这完全取决于您如何阅读/解析文件。鉴于它是csv,使用var dateValidate = function() { return { require: 'ngModel', link: function(scope, elem, attr, ngModel) { elem.bind('keyup', function(value) { var atrvls = scope.$eval(attr.datepickerOptions), maxDate = moment(atrvls.maxDate, 'DD/MM/YYYY'), minDate = moment(atrvls.minDate, 'DD/MM/YYYY'), cDate = moment(elem.val(), 'DD/MM/YYYY', true); if (!cDate.isValid()) { scope.$apply(ngModel.$setValidity('validbound', null)); return false; } if (!cDate.isBetween(minDate, maxDate, null, '[]')) { scope.$apply(ngModel.$setValidity('validbound', false)); } else { scope.$apply(ngModel.$setValidity('validbound', true)); } }); } }; }; 可能不会导致需要手动删除逗号。但是,如果您倾向于,根据您的设置,有几种方法:

如果您将每一行作为列表中的独立项目读取,则可以使用csvreader删除列表中的最后一项,或者如果将其呈现为长字符串或字符串列表,则可以只需删除最后一个字符(逗号)或迭代到该行的最后一项,然后删除最后一个字符:

.pop()

new_str = old_str[:-1] 是逗号删除的项目,new_str是整行或行中的最后一项,具体取决于您如何读取文件。然后,您可以将文件另存为csv或执行其他操作。

答案 1 :(得分:1)

简单地迭代读取和写入文件行并切掉最后两个字符(换行符\n和逗号)。可能还需要移除托架\r\n

with open('RemoveCommasInput.csv', 'r') as r, open('RemoveCommasOutput.csv', 'w') as w:    
    for num, line in enumerate(r):
        if num > 0:            
            newline = line[:-2] + "\n" if "\n" in line else line[:-1]
        else:
            newline = line               
        w.write(newline)

答案 2 :(得分:0)

如果文件名是'textsample.txt',只需在python提示符中运行

for line in fileinput.input('textsample.txt'):
    line = re.sub('(,)[^,]*$','\r', line.rstrip())
    print(line)

答案 3 :(得分:0)

在您的情况下,您只能读取第一列“ 6”,因为最后一列包含多余的逗号。要在usecols函数中使用此参数read_csv。代码如下:

import pandas as pd
pd.read_csv(path, usecols=range(6))

使用Colaboratoy的解决方案是:

import pandas as pd
from google.colab import files
import io
uploaded = files.upload()
x_train = pd.read_csv(io.StringIO(uploaded['x_train.csv'].decode('utf-8')), skiprows=1, usecols=range(6) ,header=None)