字符串与格式化
列表会追加了,看文本怎么切、怎么拼。join 方向是最容易反的。
split / join / in / strip
python
if marker not in text:
raise ValueError("...")
return text.split(marker, 1)[1].strip()
paragraphs = [
" ".join(paragraph.split())
for paragraph in transcript.split("\n\n")
if paragraph.strip()
]js
if (!text.includes(marker)) throw new Error('...')
return text.slice(text.indexOf(marker) + marker.length).trim()
const paragraphs = transcript
.split('\n\n')
.map((p) => p.trim())
.filter(Boolean)
.map((p) => p.split(/\s+/).join(' '))| Python | JavaScript |
|---|---|
s.split("\n\n") | s.split('\n\n') |
s.split(sep, 1) 最多 1 次 | indexOf + slice |
" ".join(parts) | parts.join(' ')(方向相反) |
needle in haystack | haystack.includes(needle) |
s.strip() | s.trim() |
paragraph.split() 无参 | split(/\s+/) |
f-string 与三元
python
print(f"[STEP1] total: {len(all_news)}")
item_id = f"rss_feed-{date_compact}-{idx}"
content = e.content[0].value if e.get("content") else e.get("summary", "")
status = f"待补充:{', '.join(missing)}" if missing else "信息完整"js
console.log(`[STEP1] total: ${allNews.length}`)
const itemId = `rss_feed-${dateCompact}-${idx}`
const content = e.content ? e.content[0].value : (e.summary ?? '')
const status = missing.length ? `待补充:${missing.join(', ')}` : '信息完整'| 易混 | Python | JavaScript |
|---|---|---|
| 插值 | f"...{x}..." | `...${x}...` |
| 三元顺序 | A if cond else B | cond ? A : B |
| 补零 | {n:02d} | String(n).padStart(2, '0') |
| 小数 | {x:.2f} / round(x, 2) | n.toFixed(2) |
python
node_name = f"extract_{index + 1:02d}" # extract_01js
const nodeName = `extract_${String(index + 1).padStart(2, '0')}`下一页叠:循环、带序号、推导式一次写出 map/filter。