python - 在同一列标题下拉出下一个值

标签 python mysql python-3.x csv

我正在使用Python的csv模块来读取“.csv”文件并将它们解析为MySQL插入语句。为了维护语句的语法,我需要确定每个列标题下列出的值的类型。但是,我遇到了一个问题,因为某些行以 null 值开头。

如何使用 csv 模块返回同一列下的下一个值,直到返回的值 null?这不必通过 csv 模块来完成;我对所有解决方案持开放态度。查看文档后,我不确定 csv 模块是否能够完成我需要的操作。我在想一些事情:

if rowValue == '':
    rowValue = nextRowValue(row)

显然,next() 方法只是返回 csv“列表”中的下一个值,而不是像我想要的那样返回同一列下的下一个值,并且 nextRowValue() 对象不存在。我只是在演示这个想法。

编辑:只是为了添加一些上下文,这是我正在做的事情和遇到的问题的示例。

如果表格如下:

ID Date Time  Voltage Current Watts
0  7/2  11:15         0       0
0  7/2  11:15         0       0
0  7/2  11:15  380    1       380

这是一个非常精简的代码版本,我用它来读取表格、获取列标题并确定第一行中值的类型。然后将它们放入单独的列表中,然后使用deque将它们添加到单独的函数中插入语句。并非所有代码都具有特色,我可能遗漏了一些关键部分,但这里有一个示例:

import csv, os
from collections import deque

def findType(rowValue):
    if rowValue == '':
        rowValue = 
    if '.' in rowValue:
        try:
            rowValue = type(float(rowValue))
        except ValueError:
            pass
    else:
        try:
            rowValue = type(int(rowValue))
        except:
            rowValue = type(str(rowValue))
    return rowValue

def createTable():
    inputPath = 'C:/Users/user/Desktop/test_input/'
    outputPath = 'C:/Users/user/Desktop/test_output/'
    for file in os.listdir(inputPath):
        if file.endswith('.csv'):
            with open(inputPath + file) as inFile:
                with open(outputPath + file[:-4] + '.sql', 'w') as outFile:
                    csvFile = csv.reader(inFile)
                    columnHeader = next(csvFile)
                    firstRow = next(csvFile)
                    cList = deque(columnHeader)
                    rList = deque(firstRow)
                    hList = []
                    for value in firstRow:
                        valueType = findType(firstRow)
                        if valueType == str:
                            try:
                                val = '`' + cList.popleft() + 'varchar(255)'
                                hList.append(val)
                            except IndexError:
                                pass
                        etc.

对于 findType 函数返回的其余值类型,依此类推。问题是,当使用 deque 将值添加到 rList 时,它会跳过 null 值,因此列标题列表中的项目数将为 6,例如,并且行列表中的项目数将为 5,因此它们不会对齐。

一个有点冗长的解决方案是扫描每一行的 null 值,直到使用如下方式找到一个值:

for value in firstRow:
     if value == '':
         firstRow = next(csvFile)

并继续此循环,直到找到没有 null 值的行。然而,这似乎是一个有点冗长的解决方案,会减慢程序的速度,因此我正在寻找不同的解决方案。

最佳答案

我发现跳过包含任何 null 值的行更容易,而不是像标题所示那样从列中提取下一个值。有两种不同的方法可以做到这一点:

使用循环扫描每一行并查看它是否包含 null 值,然后跳转到下一行,直到找到不包含 null 值的行。例如:

tempRow = next(csvFile)
for value in tempRow:
    if value == '':
       tempRow = next(csvFile)
    else:
       row = tempRow

关于python - 在同一列标题下拉出下一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38573287/

相关文章:

python - 比较 pandas 中的日期时间的最快方法是什么?

javascript - Splash 无法获取整个页面

python - 如何从字典中获取 pandas DataFrame?

python - 如何在发送给客户端之前使用squid代理服务器修改html页面

python - Docker - 无法访问 Django 服务器

python - 是否可以使用类似缓冲区(基于指针)的字符串比较在 python 3 中进行排序?

python - 在Python中将一个循环包装在另一个循环中?

javascript - 数据不显示

mysql - REGEXP_REPLACE 不翻译反向引用

php - Laravel + 存储过程 : Error Packets out of order