- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Psycopg2 将 CSV 文件中的数据动态导入到 PostGreSQL 数据库中。我的 CSV 文件 header 解析正确,但数据被逗号挂起并引发错误。
此数据将用于 Azure 数据库实例。我试过从 .join
语句中删除逗号,但这搞砸了格式并仍然抛出类似的错误。我还填写了缺失的数据,因为我认为情况也可能如此。
这是我使用的数据示例:
first_name,Last_name,mothers_maiden_name,date_of_birth,participant_age,gender,homeless,runaway,training_program,Created
Tiffany,Wilson,N/A,02-05-1994,24,Female,N/A,N/A,Employment Training Assistance ,12-14-2018 12:44:06
代码片段:
with open('Assistance Request Form-participant-Info.csv', 'r') as f:
reader = csv.reader(f)
columns = next(reader)
query = 'insert into participant({0}) values({1})'
query = query.format(','.join(columns), ','.join('?' * len(columns)))
print(query)
cursor = connection.cursor()
for data in reader:
cursor.execute(query,data)
cursor.commit()
和错误消息/StackTrace:
psycopg2.ProgrammingError: syntax error at or near ","
LINE 1: ...omeless,runaway,training_program,Created) values(?,?,?,?,?,?...
预期的输出应该是插入语句将每个数据点动态加载到表的正确位置。
最佳答案
使用 '%s'
作为 cursor.execute()
中的占位符。
query = query.format(','.join(columns), ','.join(['%s'] * len(columns)))
cursor = connection.cursor()
for data in reader:
cursor.execute(query,data)
connection.commit() # not cursor.commit()
关于python - 为什么 Psycopg2 很难在 CSV 中解析我的逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54019299/
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
我有一个带有输入字段的表单,使用javascript,当用户输入超过2个字符时,它会在第一个输入字段下方创建相同的输入字段。其代码是: Optie 1: 1 && treated[this.na
这是我的: char userInput; int position; vector userVector(7); vector someVector(7,1); cin >> userInput;
尝试使用 typescript 和 redux 构建一个简单的 react crud 应用程序并遇到以下问题。我有一个具有指定签名的函数,它将一个人对象作为参数,如此处所示。 export defau
哦,我多么希望 TCP 像 UDP 一样基于数据包! [查看评论] 但是,唉,事实并非如此,所以我正在尝试实现我自己的数据包层。这是到目前为止的事件链(忽略写入数据包) 哦,我的数据包结构非常简单:两
我想在我的页面底部放置一个包含不同数量图片的栏,这些图片(如果比页面宽)可以左右滚动。 页面宽度在变化,我希望 Pane 的宽度为 100%。 我试图通过让中间的 div 溢出并使用 jquery.a
我曾尝试在工作时将我的 Rails 应用程序 bundle 到我的 Mac 上。在家里它运行良好,我之前已经设法自己解决了它,但这次无论我尝试什么似乎都无法解决它。 我在运行 bundle/bundl
所以我有一个旧的网络表单站点,并且正在努力使其更易于维护。把它扔掉并重写它不是一种选择。 IoC 显然是它首先得到的东西之一,但这给我留下了服务定位器模式和糟糕的品味,并且想知道它是否可以做得更好。
我是一名优秀的程序员,十分优秀!