将OpenWeatherMap预报JSON转换为CSV适配Gnuplot的技术问询
Hey there! Nice job getting this far with json2csv—flattening the data and stripping irrelevant fields is half the battle. Let's walk through both of your requested solutions to get a clean CSV ready for Gnuplot:
Option 1: Use sed to Add Line Breaks After Plain-Text Dates
If your current flattened output has all forecast entries crammed into one line (with plain-text dates like YYYY-MM-DD acting as entry separators), you can use sed to split them into proper CSV rows.
Example Command
Assuming your flattened text has entries separated by dates in YYYY-MM-DD format (e.g., 2024-05-20,25,602024-05-21,26,55):
# Insert a line break before each date, then remove any leading empty line sed -E 's/([0-9]{4}-[0-9]{2}-[0-9]{2})/\n\1/g' your_flattened_output.txt | sed '/^$/d' > formatted_forecasts.csv
Customization Tips
- If your dates are paired with a field name (e.g.,
date:2024-05-20), adjust the regex to match that pattern:sed -E 's/(date:[0-9]{4}-[0-9]{2}-[0-9]{2})/\n\1/g' your_flattened_output.txt | sed '/^$/d' > formatted_forecasts.csv - Tweak the regex to match your exact date format (e.g., add time if needed:
[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}).
Option 2: Directly Flatten JSON to CSV (No Post-Processing Needed)
Instead of fixing a messy flattened output, use a tool like jq (more flexible than basic json2csv) or refined json2csv arguments to generate a properly formatted CSV directly from your JSON.
Using jq (Highly Recommended for Control)
jq lets you explicitly select fields and structure the CSV perfectly for Gnuplot. Suppose your JSON looks like this:
{ "weather_data": [ {"date": "2024-05-20", "temp_c": 25, "humidity": 60}, {"date": "2024-05-21", "temp_c": 26, "humidity": 55} ] }
Run this command to output a CSV with headers and properly separated rows:
jq -r '["date", "temp_c", "humidity"], (.weather_data[] | [.date, .temp_c, .humidity]) | @csv' input.json > gnuplot_ready.csv
Using json2csv (Refined Arguments)
If you prefer sticking with json2csv, use the --unwind flag to expand nested arrays and --fields to specify exactly what you want:
json2csv -i input.json -f date,temp_c,humidity --unwind weather_data > gnuplot_ready.csv
Notes
- Replace
weather_datawith the actual name of your nested array in the JSON. - Adjust the field list (
date,temp_c,humidity) to match your data's keys.
Either approach will give you a clean CSV that Gnuplot can read easily. Just tweak the regex/field paths to match your specific JSON structure!
内容的提问来源于stack exchange,提问作者user277387

