【问题标题】:How to replace a dummy name if a csv file contains a blank field?如果 csv 文件包含空白字段,如何替换虚拟名称?
【发布时间】:2017-06-07 14:39:21
【问题描述】:

这是一个包含多行的 CSV 文件,但问题是每行包含 6 个字段,但有些字段包含一些缺失的字段。因此,在将结果上传到数据库时,我想用墓碑替换缺失的字段像'NA'。

这里是我写的代码。

with open("results/somename.csv","r") as f:
        for record in f:
            #print record.split()[0]
            record = record.split("|")
            file1 = record[0]
            file2 = record[1]
            file3 = record[2]
            file4 = record[3]
            file5 = record[4]
            if not record[5] :
                file6 = record[5]
            else:
                file6 = 'NA'
            if not record[6] :
                file7 = record[6]
            else:
                file7 =  'NA'

这里我根据分隔符|拆分文件,然后尝试将文件拆分成可以在数据库中使用的字段。现在由于信息不足记录[5]和记录[6]不可用在某些行中(不是全部)。所以我正在检查它是否包含任何字符串,否则替换为'NA'。但是在执行过程中出现了错误。

Traceback (most recent call last):
  File "db_kiran.py", line 15, in <module>
    if not record[5] :
IndexError: list index out of range

所以我只想在缺失的字段中替换为“NA”。

【问题讨论】:

  • 请检查拆分后数组的长度。使用 if 来执行预期的操作。
  • @SanketSudake 谢谢。
  • @e4c5 感谢您的回答,我已经使用了第一条评论并更改了我的代码。
  • 但请考虑改用 LOAD DATA INFILE

标签: python mysql csv


【解决方案1】:

使用以下代码

with open("results/somename.csv","r") as f:
    for record in f:
        #print record.split()[0]
        record = record.split("|")
        file1 = record[0]
        file2 = record[1]
        file3 = record[2]
        file4 = record[3]
        file5 = record[4]
        try:
            file6 = record[5]
        except IndexError as e:
            file6 = 'NA'
            file7 = 'NA'
            continue
        try:
            file7 = record[6]
        except IndexError as e:
            file7 = 'NA'
       'do something'

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    为了数据库本身支持的东西默认,付出了很多努力。只需将相关列更改为默认值 NA。这将确保当您在其中插入 NULL 时该列显示为 NA

    ALTER TABLE Table1 ALTER COLUMN my_column set DEFAULT 'NA'
    

    这使得很多代码变得多余,但是等等,您不需要手动遍历文件并逐行拆分它们来执行此操作。 Python 对CSV 有很好的支持,可以将代码减少几行。

    等等……

    您可以在单个 mysql 命令中执行此操作。甚至不需要一行 python 代码

    LOAD DATA INFILE 'somename.csv' INTO TABLE 'Table1'
    FIELDS TERMINATED BY '|'
    

    LOAD DATA INFILE 语句将文本文件中的行读入 以非常高的速度工作。

    【讨论】:

      【解决方案3】:

      在 Jay 的代码中添加通用方法 ...

      def get_file(record, index, default="N/A"):        
          try:
               if not record[index]: # not sure above this line, copied from your code
                   return record[index]
          except IndexError:
               return default
      
      
      with open("results/somename.csv","r") as f:
              # <your code>...
      
              # Posible index error
              file6 = get_file(record, 5)
              # Even you can check here 
              # if file6 == 'N/A':
              #    <code to return all next files as N/A>
              file7 = get_file(record, 6)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-31
        • 2012-05-08
        • 2018-11-17
        • 2021-06-18
        • 2021-05-06
        • 1970-01-01
        • 2017-07-18
        相关资源
        最近更新 更多