我有一個“大”CSV 檔案,我想對其中的一個“值”進行計算。
從 csv 中提取:
id,active,source,SourceId,latlngs,type,area,fir,Designator,MultipleCode,Name
142,0,N,4204260,"-17652,-32400;-17656,-32229;-17762,-32230;-17730,-32400;",R,"",SBRJ,,"","Area 1 "
264040,0,N,10083080,"29900,8400;29900,10800;29650,10800;29650,8400;"," ","R ","Area 2 "
并且檔案持續了許多兆位元組......
我正在執行以下操作:
import csv
with open('example.csv', mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
print(row["Name"],end=',')
f= row["latlngs"][0:-1].split(sep=';')
for a in f:
b= a.split()[0:]
print (b)
結果我得到:
Area1 ,['-17652,-32400']
['-17656,-32229']
['-17762,-32230']
['-17730,-32400']
Area 2 ,['-17652,-32400']
['-17656,-32229']
['-17762,-32230']
['-17730,-32400']
現在我latlngs在名稱旁邊有了下一步,例如,我需要劃分串列中的每個值;[-17652,-32400'] / 600擁有-29.42 -54或['-17656,-32229'] / 600擁有-29.426 -53.715。
在這樣做時,是我失敗的地方。在我添加的最后一行代碼之后,我嘗試了很多東西。
for a in f:
a.split(',')[0:]
x = a[0:].split(sep=',')
try:
y = int(x[0])/600
z = int(x[1])/600
print (y,z)
except ValueError as e:
print (e)
但是在那里,得到計算的是latlngsCSV 檔案中的最后一個。
從輸出中提取:
Area 500,['32390,-8980']
['31920,-9230']
['31930,-9290']
['32510,-12220']
['33090,-18000']
['32510,-23780']
['31330,-29680']
['31330,-29700']
['30620,-32380']
['30620,-32380']
['31070,-32730']
['31530,-33060']
['32260,-30310']
['33480,-24220']
['33480,-24210']
['34090,-18090']
['34090,-17900']
['33480,-11780']
['33470,-11740']
['32870,-8720']
['32390,-8980']
53.983333333333334 -14.966666666666667
53.2 -15.383333333333333
53.21666666666667 -15.483333333333333
54.18333333333333 -20.366666666666667
55.15 -30.0
54.18333333333333 -39.63333333333333
52.21666666666667 -49.46666666666667
52.21666666666667 -49.5
51.03333333333333 -53.96666666666667
51.03333333333333 -53.96666666666667
51.78333333333333 -54.55
52.55 -55.1
53.766666666666666 -50.516666666666666
55.8 -40.36666666666667
55.8 -40.35
56.81666666666667 -30.15
56.81666666666667 -29.833333333333332
55.8 -19.633333333333333
55.78333333333333 -19.566666666666666
54.78333333333333 -14.533333333333333
53.983333333333334 -14.966666666666667
我能看到(在我有限的視野中)是我無法理解為什么計算和顯示的值是最后一個而不是每一個。
主要目標是:
一旦我得到這些計算的結果,我會將它們存盤到一個新的 CSV 檔案中,“最終目標”將是:
Name,latlngs
Area 1, "53.983333333333334 -14.966666666666667,53.2 -15.383333333333333,53.21666666666667 -15.483333333333333,54.18333333333333 -20.366666666666667,55.15 -30.0"<br/>
Area 2,"49.833333333333336 14.0,49.833333333333336 18.0,49.416666666666664 18.0,49.416666666666664 14.0"<br/>```
uj5u.com熱心網友回復:
鑒于您想重新格式化 CSV 檔案中坐標的樣式(并將值除以 600),這可能有效:
import csv
with (open('example.csv', newline='', encoding='utf-8') as infile,
open('output.csv', 'w', newline='', encoding='utf-8') as outfile):
reader = csv.DictReader(infile)
writer = csv.DictWriter(outfile, fieldnames=['area', 'coordinates'])
writer.writeheader()
for row in reader:
# Split coordinates and divide by 600
coordinates = [[int(value)/600 for value in coord.split(',')] for coord in row['latlngs'][:-1].split(';')]
# Format coordinates into new format
coordinates = ','.join(f"{coord[0]} {coord[1]}" for coord in coordinates)
writer.writerow({'area': row['Name'], 'coordinates': coordinates})
它的輸出為
Name,latlngs
Area 1 ,"-29.42 -54.0,-29.426666666666666 -53.715,-29.60333333333333 -53.71666666666667,-29.55 -54.0"
uj5u.com熱心網友回復:
您不能劃分串列:劃分將在串列上操作,而不是在串列中的單個專案上。對于該功能,您需要使用 NumPy(在這種情況下,您也可以開始使用 Pandas 來處理表格資料)。
在純 Python 中,使用 CSV 模塊,這樣的事情可以作業:
import csv
with open('example.csv', newline='', encoding='utf-8') as fp:
reader = csv.DictReader(fp)
for row in reader:
print(row['Name'], end=', ')
coords = row['latlngs'][:-1].split(';')
for coord in coords:
print(coord)
lat, lon = [int(item) for item in coord.split(',')]
lat /= 600
lon /= 600
print(lat, lon)
然后,您需要為每一行存盤每個單獨的緯度和經度(可能在一個串列中),然后將該緯度和經度串列存盤在整個檔案的另一個結構中。或者直接將它們寫入新檔案。
uj5u.com熱心網友回復:
如果您可以自由使用該pandas庫,則可以嘗試這樣的事情。
from pandas import read_csv, DataFrame
def transform(lat_long: str):
# -17652,-32400;-17656,-32229;-17762,-32230;-17730,-32400;
raw = [ll.split(",") for ll in lat_long.split(';') if ll]
new_lat_longs = [(float(lat) / 600, float(long) / 600) for lat, long in raw]
new_lat_longs = ";".join([f"{lat}, {long}" for lat, long in new_lat_longs])
return new_lat_longs
df: DataFrame = read_csv('sorted.csv', index_col=0, header=0)
df["new_latlngs"] = df["latlngs"].apply(transform)
df.to_csv('transformed_new.csv')
這基本上是加載 CSV,將轉換函式應用于所有行中的每個 latlng 列,并將值設定為新列。
新的 CSV 看起來像
id,active,source,SourceId,latlngs,type,area,fir,Designator,MultipleCode,Name,new_latlngs
142,0,N,4204260,"-17652,-32400;-17656,-32229;-17762,-32230;-17730,-32400;",R,,SBRJ,,,Area 1 ,"-29.42, -54.0;-29.426666666666666, -53.715;-29.60333333333333, -53.71666666666667;-29.55, -54.0"
264040,0,N,10083080,"29900,8400;29900,10800;29650,10800;29650,8400;", ,R ,Area 2 ,,,,"49.833333333333336, 14.0;49.833333333333336, 18.0;49.416666666666664, 18.0;49.416666666666664, 14.0"
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/462316.html
