- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将数组转换为 csv,以便我可以快速将商品导入 Shopify。 According to Shopify ,您必须执行以下操作才能在导入时添加多张图片:
因此,第一张图片排在第一行,所有后续图片排在下面几行。 CSV 示例位于此处:https://help.shopify.com/csv/product_template.csv
因此,我想编写一些程序来循环遍历数组,如下所示(除了明显更长),并将其转换为 CSV,将除第一张照片之外的所有照片放入新行。
var array = [
{
"brief": "Brief 1",
"description": "Description 1",
"photos": [
"https://cdn.shopify.com/s/files/1/01/01/01/files/imgs-example.jpg?0101010101010",
"https://cdn.shopify.com/s/files/1/01/01/01/files/imgs-example2.jpg?0101010101010",
"https://cdn.shopify.com/s/files/1/01/01/01/files/imgs-example3.jpg?0101010101010"
],
"price": "145",
"tags": [
"tag1",
"tag2",
"tag3"
],
"title": "Title 1"
},
{
"brief": "Brief 2",
"description": "Description 2",
"photos": [
"https://cdn.shopify.com/s/files/1/01/01/01/files/imgs-example4.jpg?0101010101010",
"https://cdn.shopify.com/s/files/1/01/01/01/files/imgs-example5.jpg?0101010101010"
],
"price": "150",
"tags": [
"tag4",
"tag5",
"tag6",
"tag7",
"tag8",
],
"title": "Title 2"
}
]
我通常用 json2csv 做这种事情,看起来像下面这样,只是我不确定如何处理多行方面。
json2csv -i data_in.json -f title,description,price,etc -o data_out.csv
Python 也可能是一个不错的选择,如下所示 this post ,但多行方面再次令人困惑:
import csv
import json
x = """[
{
"brief": "Brief 1",
"description": "Description 1",
"photos": [
"https://cdn.shopify.com/s/files/1/01/01/01/files/imgs-example.jpg?0101010101010",
"https://cdn.shopify.com/s/files/1/01/01/01/files/imgs-example2.jpg?0101010101010",
"https://cdn.shopify.com/s/files/1/01/01/01/files/imgs-example3.jpg?0101010101010"
],
"price": "145",
"tags": [
"tag1",
"tag2",
"tag3"
],
"title": "Title 1"
},
{
"brief": "Brief 2",
"description": "Description 2",
"photos": [
"https://cdn.shopify.com/s/files/1/01/01/01/files/imgs-example4.jpg?0101010101010",
"https://cdn.shopify.com/s/files/1/01/01/01/files/imgs-example5.jpg?0101010101010"
],
"price": "150",
"tags": [
"tag4",
"tag5",
"tag6",
"tag7",
"tag8",
],
"title": "Title 2"
}
]"""
x = json.loads(x)
f = csv.writer(open("example.csv", "wb+"))
# Write CSV Header, If you dont need that, remove this line
f.writerow(["title", "description", "price"])
for x in x:
f.writerow([x["title"],
x["description"],
x["price"])
CSV 的输出行顶部如下(必须“运行代码段”才能看到表格)- 请注意我不是要创建 HTML 表格:
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}
.tg .tg-yw4l{vertical-align:top}
</style>
<table class="tg">
<tr>
<th class="tg-yw4l">Handle</th>
<th class="tg-yw4l">Title</th>
<th class="tg-yw4l">Body (HTML)</th>
<th class="tg-yw4l">Vendor</th>
<th class="tg-yw4l">Type</th>
<th class="tg-yw4l">Tags</th>
<th class="tg-yw4l">Published</th>
<th class="tg-yw4l">Option1 Name</th>
<th class="tg-yw4l">Option1 Value</th>
<th class="tg-yw4l">Option2 Name</th>
<th class="tg-yw4l">Option2 Value</th>
<th class="tg-yw4l">Option3 Name</th>
<th class="tg-yw4l">Option3 Value</th>
<th class="tg-yw4l">Variant SKU</th>
<th class="tg-yw4l">Variant Grams</th>
<th class="tg-yw4l">Variant Inventory Tracker</th>
<th class="tg-yw4l">Variant Inventory Qty</th>
<th class="tg-yw4l">Variant Inventory Policy</th>
<th class="tg-yw4l">Variant Fulfillment Service</th>
<th class="tg-yw4l">Variant Price</th>
<th class="tg-yw4l">Variant Compare At Price</th>
<th class="tg-yw4l">Variant Requires Shipping</th>
<th class="tg-yw4l">Variant Taxable</th>
<th class="tg-yw4l">Variant Barcode</th>
<th class="tg-yw4l">Image Src</th>
<th class="tg-yw4l">Image Alt Text</th>
<th class="tg-yw4l">Gift Card</th>
<th class="tg-yw4l">Google Shopping / MPN</th>
<th class="tg-yw4l">Google Shopping / Age Group</th>
<th class="tg-yw4l">Google Shopping / Gender</th>
<th class="tg-yw4l">Google Shopping / Google Product Category</th>
<th class="tg-yw4l">SEO Title</th>
<th class="tg-yw4l">SEO Description</th>
<th class="tg-yw4l">Google Shopping / AdWords Grouping</th>
<th class="tg-yw4l">Google Shopping / AdWords Labels</th>
<th class="tg-yw4l">Google Shopping / Condition</th>
<th class="tg-yw4l">Google Shopping / Custom Product</th>
<th class="tg-yw4l">Google Shopping / Custom Label 0</th>
<th class="tg-yw4l">Google Shopping / Custom Label 1</th>
<th class="tg-yw4l">Google Shopping / Custom Label 2</th>
<th class="tg-yw4l">Google Shopping / Custom Label 3</th>
<th class="tg-yw4l">Google Shopping / Custom Label 4</th>
<th class="tg-yw4l">Variant Image</th>
<th class="tg-yw4l">Variant Weight Unit</th>
<th class="tg-yw4l"></th>
<th class="tg-yw4l"></th>
</tr>
<tr>
<td class="tg-yw4l">Title 1</td>
<td class="tg-yw4l">Title 1</td>
<td class="tg-yw4l">Description 1</td>
<td class="tg-yw4l">Vendor Name</td>
<td class="tg-yw4l">Product Type</td>
<td class="tg-yw4l">"tag1,tag2,tag3"</td>
<td class="tg-yw4l">TRUE</td>
<td class="tg-yw4l">Title 1</td>
<td class="tg-yw4l">Default Title</td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l">1</td>
<td class="tg-yw4l">deny</td>
<td class="tg-yw4l">manual</td>
<td class="tg-yw4l">145</td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l">TRUE</td>
<td class="tg-yw4l">TRUE</td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l">https://cdn.shopify.com/s/files/1/01/01/01/files/imgs-example.jpg?0101010101010</td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
</tr>
<tr>
<td class="tg-yw4l">Title 1</td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l">https://cdn.shopify.com/s/files/1/01/01/01/files/imgs-example2.jpg?0101010101010</td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
</tr>
<tr>
<td class="tg-yw4l">Title 1</td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l">https://cdn.shopify.com/s/files/1/01/01/01/files/imgs-example3.jpg?0101010101010</td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
<td class="tg-yw4l"></td>
</tr>
</table>
最佳答案
这不是特别难。您的代码几乎是正确的(假设列的布局是您想要的,并且假设您传递了有效的 JSON,因为您的代码有一个额外的逗号),您只需要添加一点:
for x in x:
images = x["photos"]
f.writerow([x["title"],
x["description"],
x["price"],
images.pop(0) if images else None])
while images:
f.writerow([None, None, None, images.pop(0)])
简而言之,它的作用是按顺序遍历所有图像,并将它们打印在新的、否则为空的行中。在您的原始代码上运行它,example.csv
最终看起来像这样:
title,description,price
Title 1,Description 1,145,https://cdn.shopify.com/s/files/1/01/01/01/files/imgs-example.jpg?0101010101010
,,,https://cdn.shopify.com/s/files/1/01/01/01/files/imgs-example2.jpg?0101010101010
,,,https://cdn.shopify.com/s/files/1/01/01/01/files/imgs-example3.jpg?0101010101010
Title 2,Description 2,150,https://cdn.shopify.com/s/files/1/01/01/01/files/imgs-example4.jpg?0101010101010
,,,https://cdn.shopify.com/s/files/1/01/01/01/files/imgs-example5.jpg?0101010101010
如何将内容放入不同的列应该是非常明显的——None
只是为了让 csv.writer
将单元格留空,所以将它们用作所有其他列的占位符。
如果这需要易于修改,我会推荐一种不同的方法。不过,这看起来像是一次性转换脚本,因此快速、简单的破解就可以了。
一些注意事项:
a if b else c
,如果b
的计算结果为True
,表示a
;如果计算结果为 False
,则表示 c
。ary.pop(n)
从 ary
中弹出(删除并返回)索引为 n
的对象。False
(因此我只测试 images
,而不是 len(images) == 0
)while
;您可能希望修复它们以准确标记列。关于python - 将数组转换为 CSV,包括多行图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51214273/
127.0.0.1:8000/api/仅包含来自第二个应用程序的 url,但我将两个 url 模块链接到相同的模式。甚至有可能做到这一点吗? 第一个应用程序: from django.urls imp
我目前正在学习 ColdFusion。我有 PHP 背景,对此我有点困惑。 我有一个选择菜单,我希望将选项保存在不同的文件中。 (例如 options.cfm)当我调用文件时,我想在选择菜单中包含选项
字符串: "75,000", "is", "95%", "or", "95/100" "of", "monthly", "income" o/p: "is","%, "or", "/", "of",
我有 4 个 javascript 文件(每个文件对应一个 HTML 文件),所有 4 个文件中的 3 个函数都是相同的。我想找到一个顺利的解决方案,我可以以某种方式分别包含这 3 个函数...是否可
我在 PHP 中有这种情况,其中 include在一台服务器上被遗漏,但在另一台服务器上没有(我没有设置服务器,所以我不能告诉你更多;我不是真正的 devops 人,所以这就是我在这里问的原因)。两台
这是一个模式文件,midi.xsd定义类型,note ,用于存储 MIDI 音符值: 这是另一个模式文件,octaves.xsd使用
我想备份以下文件夹 /home /etc /usr/local /root /var /boot 并排除 /var/tmp /var/run /var/lock /home/*/.thumbnails
如何重新编码具有许多值(包括缺失值)的数值变量,以获得数字 0:n-1哪里n是唯一值的数量,包括 NA ,整齐? 例子: df 1 1000 0 2 1000 0 3 N
选择元素的 html(包括在内)的最佳方法是什么?例如: This is just a test. 而$('#testDiv').html()返回"This is just a test."
我正在尝试设置Varnish来处理本地环境中的ESI包含。 我在虚拟机中运行 Varnish ,内容在主机上运行。 我有两个文件“index.html”和“test.html”。它们都存储在apach
我有以下内容,并且想要检索“ FromEmail”不为空的数据 Simple email@gma
欧海,我正在编写一个小型 PHP 应用程序,使用一个单独的 config.php 文件和一个functions.php,其中包含我将在应用程序中使用的所有自定义函数。现在,我真的必须在每个函数中包含
我知道可以将 JavaScript 放在一个特定的 .js 文件中,然后通过执行以下操作将其包含在任何页面中...... 我注意到,对于包含的这些 .js 文件: 它们实际上不必以 .js 结尾 其
我使用 gwt UIBinder 添加了一些项目到我的 ComboBox。 --select one-- Dispute Referral Form Dispute Settlement Clause
我可以将一个 first.c 文件包含到另一个 second.c 中吗? (我正在做一些套接字编程,以将服务器收到的消息存储在链接列表中,因此在第一个程序中,我尝试保留链接列表和第二个程序套接字编程文
我有一个简单的 Spring MVC 数据项目设置,我试图选择 Admin 中尚不存在的用户列表。 table 。这是我的存储库方法 SELECT u FROM User u WHERE u.id N
在 bash 脚本中,使用什么实用程序以及如何删除两个字符串之间的文本,包括字符串。 原文: (ABC blah1)blah 2(def blah 5)blah 7)(DEF blah 8)blah
我有这个 BST 问题,我试图用 Java 解决,但我不知道为什么它不起作用。问题是: 二叉搜索树 (BST) 是一种二叉树,其中每个值节点大于或等于该节点的所有节点中的值左子树并且小于该树中所有节点
我有一个字符串,其中包含“Dollars”和“Cents”符号。我想删除它们。我试过了 string.replaceAll("[\"\\u00A2\" $]", "") 但它不起作用。正确的做法是什么
我在 stories 和 tags 之间有一个多对多的关系,为保存关系而创建的表是 taxonomies。我想搜索所有具有所有给定标签的故事。 到目前为止我使用的查询是这个,当然它对我不起作用,它返回
我是一名优秀的程序员,十分优秀!