- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这一切都有点模糊,因为该计划是相当深入的,但坚持我,因为我会尽量解释它。我编写了一个程序,它接受一个.csv
文件,并将其转换为MySQL数据库的INSERT INTO
语句。例如:
ID Number Letter Decimal Random
0 1 a 1.8 A9B34
1 4 b 2.4 C8J91
2 7 c 3.7 L9O77
INSERT INTO table_name ('ID' int, 'Number' int, 'Letter' varchar(). 'Decimal', float(), 'Random' varchar()) VALUES ('0', '1', 'a', '1.8', 'A9B34');
.csv
文件都具有相同的列标题,但它们需要插入到同一个表中。对于没有特定列标题的文件,我想插入一个
NULL
值来显示这一点。例如:
.csv
文件A包含以下信息:
ID Number Decimal Random
0 1 1.8 A9B34
1 4 2.4 C8J91
.csv
文件B具有不同的列标题:
ID Number Letter Decimal
0 3 x 5.6
1 8 y 4.8
INSERT
语句并放入数据库之后,理想情况下应该是这样:
ID TableID Number Decimal Letter Random
0 A 1 1.8 NULL A9B34
1 A 4 2.4 NULL C8J91
2 B 3 5.6 x NULL
3 B 8 4.8 y NULL
.csv
文件的所有列标题的主列表:
def createMaster(path):
global master
master = []
for file in os.listdir(path):
if file.endswith('.csv'):
with open(path + file) as inFile:
csvFile = csv.reader(inFile)
col = next(csvFile) # gets the first line of the file, aka the column headers
master.extend(col) # adds the column headers from each file to the master list
masterTemp = OrderedDict.fromkeys(master) # gets rid of duplicates while maintaining order
masterFinal = list(masterTemp.keys()) # turns from OrderedDict to list
return masterFinal
.csv
文件中获取所有列标题,并将它们按顺序组合到主列表中,而不重复:
['ID', 'Number', 'Decimal', 'Letter', 'Random']
INSERT
语句的第一部分。现在我需要将
VALUES
部分添加到语句中,所以我一次获取并列出每个
.csv
文件的每行中的所有值。为每一行创建一个临时列表,然后将该文件的列标题列表与所有文件的列标题主列表进行比较。然后它遍历主列表中的每一项,并尝试获取列列表中同一项的索引。如果在列列表中找到该项,则会将同一索引处的行列表中的项插入到临时列表中。如果找不到项,则将
'NULL'
插入临时列表。一旦它完成了临时列表,它就会以正确的MySQL语法将列表转换为字符串,并将其附加到
.sql
文件中进行插入。代码中有相同的想法:
def createInsert(inPath, outPath):
for file in os.listdir(inpath):
if file.endswith('.csv'):
with open(inPath + file) as inFile:
with open(outPath + 'table_name' + '.sql', 'a') as outFile:
csvFile = csv.reader(inFile)
col = next(csvFile) # gets the first row of column headers
for row in csvFile:
tempMaster = [] # creates a tempMaster list
insert = 'INSERT INTO ' + 'table_name' + ' (' + ','.join(master)+ ') VALUES ' # SQL syntax crap
for x in master:
try:
i = col.index(x) # looks for the value in the column list
r = row[i] # gets the row value at the same index as the found column
tempMaster.append(r) # appends the row value to a temporary list
except ValueError:
tempMaster.append('NULL') # if the value is not found in the column list it just appends the string to the row master list
values = map((lambda x: "'" + x.strip() + "'"), tempMaster) # converts tempMaster from a list to a string
printOut = insert + ' (' + ','.join(values) + '):')
outFile.write(printOut + '\n') # writes the insert statement to the file
createInsert()
从tempMaster列表中获取所有行值,并通过行将它们与
'
标记连接起来:
values = map((lambda x: "'" + x.strip() + "'"), tempMaster)
NULL
值,并且只插入
NULL
而不是
'NULL'
。
'NULL'
字符串并将其更改为
NULL
?
NULL
标记中提取
'
字符串并替换它。
def findBetween(s, first, last):
try:
start = s.index(first) + len(first)
end = s.index(last, start)
return s[start:end]
except ValueError:
print('ERROR: findBetween function failure.')
def removeNull(aList):
tempList = []
for x in aList:
if x == 'NULL':
norm = findBetween(x, "'", "'")
tempList.append(norm)
else:
tempList.append(x)
return tempList
NULL
值添加到列表中,而不必从
'
开始。这在
createInsert()
函数中。
for x in tempMaster:
if x == 'NULL':
value = x
tempMaster.append(value)
else:
value = "'" + x + "'"
tempMaster.append(value)
values = map((lambda x: x.strip()), tempMaster)
printOut = insert + ' (' + ','.join(values) + ');')
outFile.write(printOut + '\n')
MemoryError
)。所以我要征求你的意见。我很抱歉,如果这是混淆或难以遵循。请让我知道,如果是这样的话,我可以做些什么来让你更容易理解,并祝贺你把事情做到底!
最佳答案
而不是
values = map((lambda x: "'" + x.strip() + "'"), tempMaster)
values = map((lambda x: "'" + x.strip() + "'" if x!='NULL' else x), tempMaster)
for row in csvFile:
values = [] # creates the final list
insert = 'INSERT INTO ' + 'table_name' + ' (' + ','.join(master)+ ') VALUES ' # SQL syntax crap
for x in master:
try:
i = col.index(x) # looks for the value in the column list
r = row[i] # gets the row value at the same index as the found column
value.append("'"+r.strip()+"'") # appends the row value to the final list
except ValueError:
value.append('NULL') # if the value is not found in the column list it just appends the string to the row master list
value
,节省内存和CPU。
关于python - (长)从列表中的字符串中删除单引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38726986/
在线编写 yacc 代码的教程使用单引号表示分号和其他字符: ';' '+' '-' (etc) 但是在使用时: '' 在我将其更改为双引号之前,我遇到了错误: "" 相似地, '>=' '==
MySQL documentation说它应该是\'。然而,scite 和 mysql 都显示 '' 有效。我看到了,它有效。我该怎么办? 最佳答案 您引用的 MySQL 文档实际上比您提到的要多一些
MySQL documentation说它应该是\'。然而,scite 和 mysql 都显示 '' 有效。我看到了,它有效。我该怎么办? 最佳答案 您引用的 MySQL 文档实际上比您提到的要多一些
MySQL documentation说它应该是\'。然而,scite 和 mysql 都显示 '' 有效。我看到了,它有效。我该怎么办? 最佳答案 您引用的 MySQL 文档实际上比您提到的要多一些
MySQL documentation说它应该是\'。然而,scite 和 mysql 都显示 '' 有效。我看到了,它有效。我该怎么办? 最佳答案 您引用的 MySQL 文档实际上比您提到的要多一些
我一直在使用 SED (Bash shell) 转义单引号问题。 我需要做 $cfg['Servers'][$i]['password'] = ''; 进入 $cfg['Servers'][$i]['
需要将'替换为\'但这就是我得到的: >>> s = "It's nice to have an example" >>> s.replace("'", "\\'") "It\\'s nice to
我在处理连接字符串中的 ' 字符时遇到问题。 Entity Framework 抛出异常说: Format of the initialization string does not conform
我遇到了一个非常奇怪的问题,我无法设置 Content-Security-Policy 所需的单引号。我假设我运行的是旧版本的 ingress,它仅在我禁用并重新启用它 (microk8s) 后才得到
插入带有撇号的值的正确 SQL 语法是什么? Insert into Person (First, Last) Values 'Joe', 'O'Brien' 我不断收到错误,因为我认为
我有三列数据 selector label option list time you personally
我使用 Java Pattern 类将正则表达式指定为字符串。 举个例子我喜欢成为蜘蛛侠:“彼得·帕克” 应将蜘蛛侠和“Peter Parker”列为单独的标记。谢谢 try { Buffe
我正在尝试删除标记 ' (单引号)但我不知道如何。 我在做delm \'但它不起作用。我也尝试了我能想到的所有组合。 我意识到这更像是一个 Vim 脚本问题,但我在任何地方都找不到。 最佳答案 为什么
如何禁用红色突出显示的特定实例。 我尝试在单引号前输入转义符,但没有成功。我确信单引号导致了红色突出显示,因为当我删除它时,它就会消失。 代码如下: import React from 'react'
我需要输出单引号 (')。它必须是单引号而不是 ',因为这是用于向 jquery 函数提供数据的字符串的一部分。 我使用 C# 循环遍历一个对象来构建一个字符串 "var data = [['Item
我正在尝试: "l'ape"); ?> var my_javascript_object = jQuery.parseJSON(''); 我收到此错误“未捕获的语法错误:意外的标识符”。问题是
我的问题是关于传递给方法验证的“add\”反斜杠单引号,该反斜杠单代码有什么用? document.writeln('\',\'N\'))>'); 最佳答案 欢迎来到堆栈溢出。 反斜杠告诉代码这不是引
我要执行命令: xcodebuild -exportArchive -exportFormat IPA -archivePath myApp.xcarchive -exportPath myApp.i
我有一个字符串,里面有一个 ': example link text 不幸的是,这似乎不起作用。 Firebug 提示“SyntaxError:参数列表后缺少 )”,您可以看到 HTML 实体已被 '
据我所知,'mars%22%3A%22' 和 "mars%22%3A%22" 是等效的,因为没有任何内容被转义. 我创建 javscript 书签已经有一段时间了。有一次,当按原样粘贴到 Chrome
我是一名优秀的程序员,十分优秀!