因为我得到的答案与我想要实现的目标无关,所以我会尽可能简单地做出答案。
多个单独的jsons,采用相同的格式,每个jsons包含各个位置的信息。
我想要powershell做的就是这个:
{'isliy': 'isliye', 'islia': 'isliye', 'islie': 'isliye', 'jayenga': 'jayenge', 'bnaya': 'banaya', 'bnaye': 'banaye', 'dikhaye': 'dikhayein', 'haiiii': 'hai', 'lalach': 'laalach', 'upr': 'upar', 'hora': 'ho raha', 'tmhe': 'tumhe', 'Aacha': 'Acha', 'naii': 'nahi', 'jakkas': 'jhakkas', 'tmhi': 'tum hi', 'sbhi': 'sabhi', 'kum': 'kam', 'sunder': 'sundar', 'ghnte': 'ghante', 'jaooo': 'jao', 'tufan': 'toofaan', 'mei': 'mein', 'glti': 'galti', 'gayee': 'gaye', 'gli': 'gali', 'k': 'ke', 'drr': 'dar', 'bhut': 'bahut', 'bhuk': 'bhook', 'bhul': 'bhool', 'krliya': 'kar lia', 'glt': 'galat', 'drd': 'dard', 'bkwas': 'bakwaas', 'Kon': 'kaun', 'dekta': 'dekhta', 'dekte': 'dekhte', 'jayrga': 'jayega', 'smjhti': 'samjhti', 'rahti': 'rehti', 'rahte': 'rehte', 'smjhte': 'samjhte', 'Gggjjjbb': 'gajab', 'krwana': 'karvana', 'niii': 'nahi', 'krwane': 'karvane', 'achchha': 'acha', 'bejna': 'bhejna', 'Achi': 'acchi', 'Apko': 'aapko', 'bhuaji': 'buaji', 'jesi': 'jaisi', 'me': 'mein', 'ma': 'Maa', 'mt': 'mat', 'dimag': 'dimaag', 'dimak': 'deemak', 'jayda': 'zyada', 'aukat': 'aaukaat', 'aaap': 'aap', 'Abb': 'Ab', 'Abi': 'Abhi', 'pitai': }
并将其转换为:
l1 = []
l2 = []
d = {}
search = raw_input("search :")
read_file = open('/Users/abhishek/Desktop/data_merged.txt','r')
split = [line.strip() for line in read_file]
for line in split:
l1.append(line.split()[0].strip())
l2.append(' '.join(line.split()[1:]).strip())
d= dict(zip(l1,l2))
print d
for key,value in d.iteritems():
if key.startswith(search):
print d[key]
但每个人几百次。我想要的是我可以复制并粘贴到我现有的html文件中的东西,这样我就不必为每个单独的位置编写上述内容。
你可以忽略coords,我不想要那些信息,我不需要标记数组,我会将标记数组放在自己身上,因为它们的坐标与标记数组不同。
答案 0 :(得分:2)
我的答案是在别处几分钟内定制的。不管怎样,谢谢
##Auto-Generated using "PSProject Builder" Created by Matt Hamende 2018
#######################################################################
#Description: generates wireframe powershell projects
#Features:
## Define ScriptRoot
## Standard Function Libraries
## PSModule Prerequities Loader
## JSON Config File
########################################################################
#Set Default Error Handling - Set to Continue for Production
$ErrorActionPreference = "Stop"
#Define Logger Function
Function Log($message) {
"$(Get-Date -Format u) | $message"
}
#Define Script Root for relative paths
$RunDir = split-path -parent $MyInvocation.MyCommand.Definition
Log "Setting Location to: $RunDir"
Set-Location $RunDir # Sets directory
## Script Below this line #######################################################
$SystemDef = Get-ChildItem $RunDir\Data\starsystem
$SystemData = @()
Log "Importing Star System Data..."
ForEach ($star in $SystemDef) {
$SystemData += (Get-Content $star.FullName) -join "`n" | ConvertFrom- Json
}
Log "System Data Imported..."
ForEach($System in $SystemData[0..9]){
""
$Tags = ($System.Tags.items -join ", ").Replace("planet_","").Replace("_"," ")
$Employers = $System.ContractEmployers -join ", "
$Biomes = $System.SupportedBiomes -join ", "
$SystemStr = @"
<p>System Name: $($System.Description.Name)</p>
<p>Star Type: $($System.StarType)
<p>Description: $($System.Description.Details)</p>
<p>Owner: $($System.Owner)</p>
<p>Tags: $Tags</p>
<p>Employers: $Employers</p>
<p>Biomes: $Biomes</p>
"@
$SystemStr
}
答案 1 :(得分:1)
Powershell解决方案:
function Convert-JsonToHTML {
param(
$json )
$jObject = ConvertFrom-Json $json
$stores = ''
$climate = ''
$itemCollection = $jObject.tags.items
foreach( $item in $itemCollection ) {
$tag = $item -split '_'
switch( $tag[0] ) {
'store' {
$storename = ''
for( $i = 1; $i -lt $tag.Count; $i++ ) {
$storename += $tag[$i].Substring(0,1).ToUpper() + $tag[$i].Substring(1).ToLower()
}
$stores += $storename + ', '
break
}
'climate' {
$climate = ''
for( $i = 1; $i -lt $tag.Count; $i++ ) {
$climate += $tag[$i].Substring(0,1).ToUpper() + $tag[$i].Substring(1).ToLower() + ' '
}
$climate = $climate.Trim()
break
}
}
}
if( $stores.Length -gt 2 ) {
$stores = $stores.Substring( 0, $stores.Length - 2)
}
$out = "var $($jObject.Description.Id) = L.marker(map.unproject([,], map.getMaxZoom()))" +
".bindPopup(`<h1>$($jObject.Description.Name)</h1><p>$($jObject.Description.Blurb)</p> \n" +
"<p>Climate: $($climate)\n" +
"<p>Stores: $($stores)\n" +
"<p>Authority: $($jObject.Description.Authority) `);"
return $out
}
$json = '{ "Description": {
"Id": "town_breezeholme",
"Name": "Breezeholme",
"Blurb": "This quiet town on the outskirts has prospered almost
completely independently from the surrounding civilisation
for nearly 200 years due to its vast wealth accumulated
from rich, fertile farmlands, rivers and shipping ports.",
"Authority": "Jeraxian Warriors"
},
"coords": {
"x": 66.4,
"y": -418.2,
"z": 34
},
"tags": {
"items": [
"store_jewelers",
"store_bank",
"store_general_goods",
"store_post_office",
"climate_sub_tropical"
]}
}'
Convert-JsonToHTML -json $json
答案 2 :(得分:0)
然后我会分享我的节点解决方案,没有人要求,但我认为这将是一个很好的练习。
import fs from 'fs';
import breezeholme from './towns/breezeholme.json';
import town2 from './towns/town2.json';
import town3 from './towns/town3.json';
let towns = [breezeholme, town2, town3];
const capitalizeTags = function(tagItems, key) {
return tagItems
.filter(tag => tag.startsWith(key))
.map(tag =>
tag
.replace(key, '')
.split('_')
.map(word => word[0].toUpperCase() + word.substring(1))
.join(' ')
)
.join(', ');
};
towns = towns.map(town => {
const {x, y} = {...town.coords};
const {Id, Name, Blurb, Authority} = {...town.Description};
const climate = capitalizeTags(town.tags.items, 'climate_');
const stores = capitalizeTags(town.tags.items, 'store_');
const html = `<h1>${Name}</h1>
<p>${Blurb}</p>
<p>Climate: ${climate}</p>
<p>Stores: ${stores}</p>
<p>Authority: ${Authority}</p>`;
return `var ${Id} = L.marker(map.unproject([${x}, ${y}], map.getMaxZoom())).bindPopup(\`${html}\`);`;
});
fs.writeFile('./MyGeneratedJavascript.js', towns.join('\n'), console.error);
我永远陷入了对这些标签进行大写的问题,而且它仍然像罪恶一样丑陋。我更喜欢你的powershell解决方案。吸取了教训。非常有趣。