如何从CSV导入

时间:2019-02-03 01:05:23

标签: python csv

我正在尝试从多个* .csv文件中解析数据,并将它们另存为列表,以备日后使用,但一直失败。

我已经阅读了SO和其他网站上的大量教程和相关主题,但是找不到解决我问题的方法。经过几天的代码处理,我陷入了困境,不知道如何继续。

# saves filepaths of *.csv files in lists (constant)
CSV_OLDFILE = glob.glob("./oldcsv/*.csv")
assert isinstance(CSV_OLDFILE, list)
CSV_NEWFILE = glob.glob("./newcsv/*.csv")
assert isinstance(CSV_NEWFILE, list)

def get_data(input):
    """copies numbers from *.csv files, saves them in list RAW_NUMBERS"""
    for i in range(0, 5): # for each of the six files
        with open(input[i], 'r') as input[i]: # open as "read"
            for line in input[i]: # parse lines for data
                input.append(int(line)) # add to list
                return input

def write_data(input):
    """writes list PROCESSED_NUMBERS_FINAL into new *.csv files"""
    for i in range(0, 5): # for each of the six files
        with open(input[i], 'w') as data: # open as "write"
            data = csv.writer(input[i])
            return data

RAW_NUMBERS = get_data(CSV_OLDFILE)
# other steps for processing data
write_data(PROCESSED_NUMBERS_FINAL)

实际结果:

TypeError: object of type '_io.TextIOWrapper' has no len()

预期结果:保存* .csv文件中的数据,对其进行处理并将其写入新的* .csv文件中。

我认为问题可能出在我试图调用len对象的file的问题上,但是我不知道正确的实现应该是什么样子。

完整回溯:

Traceback (most recent call last):
  File "./solution.py", line 100, in <module>
    PROCESSED_NUMBERS = slowsort_start(RAW_NUMBERS)
  File "./solution.py", line 73, in slowsort_start
    (input[i], 0, len(input[i])-1))
TypeError: object of type '_io.TextIOWrapper' has no len()

2 个答案:

答案 0 :(得分:1)

  

问题:预期结果:从*.csv中读取数据,处理数字并将其写入新的*.csv

OOP解决方案,将numbers保留在dict的{​​{1}}中。

  • 使用dict:listclass object初始化in_path

    out_path
  • import os, csv class ReadProcessWrite: def __init__(self, in_path, out_path): self.in_path = in_path self.out_path = out_path self.number = {} 中读取所有文件,过滤self.in_path个文件。
    使用密钥.csv创建一个dict并将该['raw']的所有numbers分配给*.csv

      

    注意:假设每行一个 list

    number
  • 处理 def read_numbers(self): for fname in os.listdir(self.in_path): if fname.endswith('.csv'): self.number[fname] = {} with open(os.path.join(self.in_path, fname)) as in_csv: self.number[fname]['raw'] = [int(number[0]) for number in csv.reader(in_csv)] print('read_numbers {} {}'.format(fname, self.number[fname]['raw'])) return self 数字并将结果关联到键['raw']

    ['final']
  • 使用相同的 def process_numbers(self): def process(numbers): return [n*10 for n in numbers] for fname in self.number: print('process_numbers {} {}'.format(fname, self.number[fname]['raw'])) # other steps for processing data self.number[fname]['final'] = process(self.number[fname]['raw']) return self 文件名将键['final']的结果写入self.out_path

    .csv
  • 用法

        def write_numbers(self):
            for fname in self.number:
                print('write_numbers {} {}'.format(fname, self.number[fname]['final']))
                with open(os.path.join(self.out_path, fname), 'w') as out_csv:
                    csv.writer(out_csv).writerows([[row] for row in self.number[fname]['final']])
    
  

输出

if __name__ == "__main__":
    ReadProcessWrite('oldcsv', 'newcsv').read_numbers().process_numbers().write_numbers()

使用Python测试:3.4.2

答案 1 :(得分:0)

这是经过反复试验和研究后我发现的解决方案:

# initializing lists for later use
RAW_DATA = [] # unsorted numbers
SORTED_DATA = [] # sorted numbers
PROCESSED_DATA = [] # sorted and multiplied numbers

def read_data(filepath): # from oldfiles
    """returns parsed unprocessed numbers from old *.csv files"""
    numbers = open(filepath, "r").read().splitlines() # reads, gets input from rows
    return numbers

def get_data(filepath): # from oldfiles
    """fills list raw_data with parsed input from old *.csv files"""
    for i in range(0, 6): # for each of the six files
        RAW_DATA.append(read_data(filepath[i])) # add their data to list

def write_data(filepath): # parameter: newfile
    """create new *.csv files with input from sorted_data and permission 600"""
    for i in range(0, 6): # for each of the six files
        with open(filepath[i], "w", newline="\n") as file: # open with "write"
            writer = csv.writer(file) # calls method for writing
            for item in SORTED_DATA[i]: # prevents data from being treated as one object
                writer.writerow([item]) # puts each entry in row
        os.chmod(filepath[i], 0o600) # sets permission to 600 (octal)

这使我可以读取文件以及创建和写入文件。鉴于我需要特定的设置,并且只有在“ A列”中才能找到数据,因此选择了此解决方案。但是再次感谢所有回答和评论的人!