- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Amazon AWS Data Pipeline 提供 CSV 文件作为 MySQL 数据库的输出。在 CSV 中,有一个包含 JSON 的字段,我们尝试分别使用 Python 的内置 CSV 和 JSON 读取器来提取和解码该字段。但是,由于 CSV 的生成方式,JSON 不以引号开头,并且 CSV 解析器仅返回该 CSV 字段的 JSON 中的第一个“{”。
我们认为 CSV 读取器会看到第一个“{”,然后会看到一个换行符,它将其解释为 CSV 行的末尾。如果 JSON 用引号引起来,则该脚本可以正常工作。请参阅以下代码:
with open(args.env_vars[0] + '/click_stream_source.csv', 'r') as csvFile:
csvReader = csv.reader(csvFile, delimiter = ',')
with open(args.env_vars[1] + '/clickstream_target.csv', 'wb') as csvTarget:
csvWriter = csv.writer(csvTarget, delimiter = ',')
for row in csvReader:
json_data = json.loads(row[5])
示例 CSV 为:
495019,,8239,E3728E7D480248AA2EB5D5BB5C467737,67.84.254.6,{
""requests"": [
{
""queryString"": null,
""time"": ""2013-06-14T11:53:40Z"",
""userAgent"": ""Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)"",
""requestURI"": ""/xxxxx/xxxx/xxxx.xxxxxxx"",
""class"": ""xxxxx"",
""params"": {
""action"": ""xxxxx"",
""controller"": ""xxxx""
},
""isAjaxRequest"": false
}]}
我们得到一个
ValueError: Expecting Object ...
其中json.loads()方法
最佳答案
我认为从技术上讲你不能调用这个 CSV,因为它违反了解析规则,但我并不是想迂腐,我想说这是放弃内置解析工具并走老派的一个原因,制作一个有限状态机。这是一个简单的示例,您可以根据自己的目的进行调整。
#!/usr/bin/env python
import re
import json
def fix_and_parse(gathered_lines):
strJson = '{' + "\n".join(gathered_lines)
strJson = strJson.replace('""', '"')
return json.loads(strJson)
state = 0
with open('csvFile', 'r') as csvFile:
gathered_lines = []
for line in csvFile:
if re.search('^\d', line):
if gathered_lines:
print json.dumps(fix_and_parse(gathered_lines), indent=4)
state = 0
gathered_lines = []
else:
state = 1
if state == 1:
gathered_lines.append(line)
print json.dumps(fix_and_parse(gathered_lines), indent=4)
关于Python CSV Reader 仅限 JSON 的第一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17118587/
class ReadLock { private: std::mutex readWriteMutex; std::mutex conditionmtx; std::condi
我在 utf-8 编码文件中有多个 messages.properties 文件(messages_en_US.properties、messages_fr.properties,...)。在这些属性
我正在尝试从 google reader api 中检索单个选定项目。是否可以通过 API 调用通过 ID 获取项目,或者我是否必须访问该项目提要并从那里获取它? 最佳答案 您可以使用 POST 到
好的,所以我有一个应用程序可以与 GR 的“api”紧密结合。 一切正常,但最近我收到来自 Google 的许可被拒绝返回。如果我退出 GR 并使用我的应用程序重新登录,一切都会重新开始。这让我相信
我想要的是最终得到类似的东西: public class InterleavedBufferedReader extends BufferedReader { ... } 并将其用作: Reader[
reader monad 有一个asks 函数,它的定义与reader 函数完全相同,为什么它作为一个单独的函数存在,与 的定义相同读者?为什么不总是使用阅读器? class Monad m => M
当使用csv模块读取文件时,有两种方法可以遍历csv.reader返回的生成器。 with open('foo.csv') as f: reader = csv.reader(f) r
我想在 Go 中按照 here 中的要求做同样的事情. 我正在解析一个巨大的日志文件,我需要逐行解析它。在每一行上,我将该行反序列化为一个结构。数据可能来自任何数据源(文件、网络等)。因此,我在我的函
我在golang的zlib/reader.go文件中找到了很多像r.(flate.Reader)这样的代码片段。这是什么意思? https://golang.org/src/compress/zlib
我正在 Spring MVC 中包装 Freemarker 模板加载器,如所述 here在 html 页面中进行默认转义。 所以,我需要用我的字符串包装来自 java.io.Reader 的内容,而不
为什么这个 PDF 在 Foxit Reader 而不是 Adobe Reader 中显示签名? 这是来自 Syncfusion PDF library 的代码用于生成它(另请参阅有关 signi
我有一个巨大的tbb::concurrent_unordered_map被多个(~60)线程同时“大量读取”。 我每天需要清除一次(完全清除或选择性清除)。在 tbb 中删除显然不是线程安全的实现,因
好像是 Hibernate.createClob(Reader reader, int length)在 3.6.x 版本中已弃用 它建议使用使用 LobHelper.createClob(Reade
这是我的实际解决方案 private def transpose[E, A](readers : Seq[Reader[E, A]]) : Reader[E, Seq[A]] = Read
DataReader[0].ToString() 和 (string)DataReader[0] 有区别吗? 我的猜测是,如果数据库类型不是字符串类型,(string)DataReader[0] 可能
我想制作一个 C# 程序来保存 pdf 和 djvu 文件的书签。如何从 AcroRd32/DjVuReader 进程中找出当前页码? 最佳答案 您可以通过 Adobe Acrobat 支持的 D
什么更好 var s = (string)reader[0] 或 var s = Convert.ToString(reader[0]) ? 最佳答案 我会说 reader.GetString(0
我对非官方阅读器 api 进行了大量研究,并筛选了其他问题,但没有一个完全满足我的要求。如果您知道文章 id 有据可查,如何分享文章,但如果您不知道 id,我想知道如何分享文章(即如何转换 url -
这是一个简单的示例,用于将 xml 文件读入 WebRowSet 对象,然后将数据从该对象加载到数据库。 import javax.sql.rowset.RowSetProvider; import
这样的转换对于任何仿函数都是可能的,不仅是Future: implicit class RichFunctorReader[F[_]: Functor, A, B](fr: F[Reader[A, B
我是一名优秀的程序员,十分优秀!