簡體   English   中英

讀取和拆分a.csv文件,該文件中包含帶逗號的字符串

[英]Reading and splitting a .csv file, which contains strings with commas in

我有一個 .csv 文件,它看起來像這樣:

1,2,"a,b",3
4,"c,d",5,6

我正在讀取並存儲在這樣的數組中:

with open(filename, 'r') as f:
    data = f.readlines()
data = [line.split(',') for line in data]

這導致這樣的數組:

[['1','2','"a','b"','3']['4','"c','d"','5','6']]

但是,我想將項目保留在雙引號中,例如數據數組的一個元素中的“a,b”(這是它們在 Excel 中打開的方式),如下所示:

[[1,2,'a,b',3][4,'c,d',5,6]]

在 Python 中是否有一種簡單的方法可以實現這一點?

編輯:如果可能的話,最好不使用 csv 模塊?

您應該使用csv模塊:

import csv

with open('test.csv') as f:
    reader = csv.reader(f)
    
    for row in reader:
        print(row)

Output:

['1', '2', 'a,b', '3']
['4', 'c,d', '5', '6']

或者,如果您不想懶惰地閱讀行並希望將所有行都放在一個列表中,就像您的問題一樣,您可以簡單地執行以下操作:

with open('test.csv') as f:
    reader = csv.reader(f)
    data = list(reader)

print(data)        
# [['1', '2', 'a,b', '3'], ['4', 'c,d', '5', '6']]   

使用csv模塊:

import csv

with open('test.csv') as file:
    reader = csv.reader(file)
    
data = [row for row in reader]

如果您不想使用csv模塊,此 function 將返回您想要的 output

def function(file_name):
    with open(file_name, 'r') as file:
        file_read = file.readlines()
        raw_data = [line.split(',') for line in file_read]

        file_data = list()
        place_0 = 0
        place_1 = 0
        ext_item = str()
        added = list()
        pre_final_list = list()
        pre_pure_list = list()
        pure_data = str()
        final_list = list()

        for List in raw_data:
            for k, v in enumerate(List):
                List[k] = v.rstrip()
        
        for line in raw_data:
            if line == ['']:
                continue
            file_data.append(line)

        for line in file_data:
            for key, value in enumerate(line):
                if '"' in value[0] and '"' in value[-1]:
                    continue
                if '"' in value[0]:
                    place_0 = key
                if '"' in value[-1]:
                    place_1 = key
                if place_1 != 0:
                    for ind in range(place_0, place_1+1):
                        added.append(line[ind])
                    for e_item in added:
                        if e_item == added[-1]:
                            ext_item += e_item
                        else:
                            ext_item += e_item + ','
                    line[place_0] = ext_item
                    for r_item_index in range(place_0+1, place_1+1):
                        line[r_item_index] = None
                    place_0 = 0
                    place_1 = 0
                    ext_item = str()
                    added = list()

        for line in file_data:
            for value in line:
                try:
                    value = int(value)
                except: 
                    pass
                if value == '\n':
                    continue
                if not value is None:
                    pre_pure_list.append(value)
            pre_final_list.append(pre_pure_list)
            pre_pure_list = list()
        

        for List in pre_final_list:
            for key, item in enumerate(List):
                if type(item) is int or '"' not in item:
                    continue
                for string in item:
                    if string == '"':
                        continue
                    pure_data += string
                List[key] = pure_data
                pure_data = str()
            final_list.append(List)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM