Python 3.4导入CSV分隔符。

4

我正在使用Python 3.4,尝试导入包含逗号、分号和制表符作为分隔符的csv文件。

是否可能让Python检测要使用哪个正确的分隔符? 我已经阅读了python: import csv file (delimiter “;” or “,”)上的帖子,但无法获得适当的结果。

到目前为止,我的代码如下:

import csv

class Data(object):
def __init__(self, csv_file):
    self.raw_data = []
    self.read(csv_file)

def read(self, csv_file):
        with open(csv_file, newline='') as csvfile:
            dialect = csv.Sniffer().sniff(csvfile.read(), delimiters=',;')
            csvfile.seek(0)
            f = csv.reader(csvfile, dialect)
            for row in f:
               self.raw_data.append(row)
            print(self.raw_data)

mycsv = Data('comma_separate.csv')

comma_separate.csv 包含:

afsfaf@faf.com, $161,321, True, 1
asafasf@fafa.net, $95.00, False, 3
adaafa3@aca.com, $952025, False, 3

现在我的输出是:

['afsfaf@faf.com, $161,321, True, 1'], ['asafasf@fafa.net, $95.00, False, 3'], ['adaafa3@aca.com, $952025, False, 3']

我的期望输出为:

['afsfaf@faf.com', '$161,321', 'True', '1'], ['asafasf@fafa.net', '$95.00', 'False', '3'], ['adaafa3@aca.com', '$952025', 'False', '3']

这可能会对你有所帮助。从CSV文件中读取数据并转换为正确的数据类型 - luoluo
2个回答

1
问题似乎在于您使用的csv文件的第一行,该行用于确定分隔符。如果您将该行更改为以下内容,则程序将按预期工作:
afsfaf@faf.com, $161.321, True, 1

我猜这是因为他想让你的CSV文件每行具有相同数量的属性。

0

对我来说,使用嗅探而不传递可能的分隔符是有效的。

import csv

class Data(object):
    def __init__(self, csv_file):
        self.raw_data = []
        self.read(csv_file)

    def read(self, csv_file):
            with open(csv_file, newline='') as csvfile:
                dialect = csv.Sniffer().sniff(csvfile.read())
                csvfile.seek(0)
                f = csv.reader(csvfile, dialect)
                for row in f:
                   self.raw_data.append(row)

                print(csvfile.name)
                print(self.raw_data)


for f in ['tab_separate.tsv','comma_separate.csv','comma_separate2.csv']:
    mycsv = Data(f)

输出

tab_separate.tsv
[['afsfaf@faf.com', '$161,321', 'True', '1'], ['asafasf@fafa.net', '$95.00', 'False', '3'], ['adaafa3@aca.com', '$952025', 'False', '3']]
comma_separate.csv
[['afsfaf@faf.com,', '$161,321,', 'True,', '1'], ['asafasf@fafa.net,', '$95.00,', 'False,', '3'], ['adaafa3@aca.com,', '$952025,', 'False,', '3']]
comma_separate2.csv
[['afsfaf@faf.com', '$161,321', 'True', '1'], ['asafasf@fafa.ne', '$95.00', 'False', '3'], ['adaafa3@aca.com', '$952025', 'False', '3']]

逗号输入

afsfaf@faf.com, $161,321, True, 1
asafasf@fafa.net, $95.00, False, 3
adaafa3@aca.com, $952025, False, 3

标签输入

afsfaf@faf.com  $161,321    True    1
asafasf@fafa.net    $95.00  False   3
adaafa3@aca.com $952025 False   3

分号输入

afsfaf@faf.com;$161,321;True;1
asafasf@fafa.ne;$95.00;False;3
adaafa3@aca.com;$952025;False;3

很奇怪,我甚至复制了你的代码和comma_separate.csv文件,但仍然给出以下输出:comma_separate.csv [['afsfaf@faf.com, $161,321, True, 1'], ['asafasf@fafa.net, $95.00, False, 3'], ['adaafa3@aca.com, $952025, False, 3']]'你也在使用Python 3.4吗? - JanV123
1
是的,再看一遍,我认为我的代码在逗号分隔文件中是按空格进行分割而不是逗号。也就是说,它认为这是一个空格分隔的文件。你能否将CSV输入文件上传到某个地方? - Brendan Doherty
https://drive.google.com/file/d/0BxeHWbvOxiOTYU15N200S3R4cVk/view?usp=sharinghttps://drive.google.com/file/d/0BxeHWbvOxiOTb1hSYTVaTl9oZUU/view?usp=sharing - JanV123
问题已解决。是MS Excel的格式错误。使用Libreoffice制作CSV文件,现在可以正常工作了。想象一下... - JanV123

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接