python - 在 python 中使用 X 和 Y 值从两个 CSV 文件创建单个 CSV

标签 python list csv geo

我有两个 CSV 文件;一个包含 X(经度)和其他 Y(纬度)值(它们是“浮点”数据类型)

我正在尝试创建包含所有可能组合的单个 CSV(例如 X1,Y1;X1, Y2;X1,Y3;X2,Y1;X2,Y2;X2,Y3...等)

我写了以下内容,部分有效。但是,创建的 CSV 文件在值之间有行,我还得到像这样存储的值,并带有列表括号 ['20.7599'] ['135.9028']。我需要的是 20.7599, 135.9028

import csv

inLatCSV = r"C:\data\Lat.csv"
inLongCSV = r"C:\data\Long.csv"
outCSV = r"C:\data\LatLong.csv"

with open(inLatCSV, 'r') as f:
  reader = csv.reader(f)
  list_Lat = list(reader)

with open(inLongCSV, 'r') as f:
   reader = csv.reader(f)
   list_Long = list(reader)

with open(outCSV, 'w') as myfile:
   for y in list_Lat:
       for x in list_Long:
            combVal = (y,x)
            #print (combVal)
            wr = csv.writer(myfile)
            wr.writerow(combVal)

最佳答案

open 函数添加一个参数会产生不同:

with open(my_csv, 'w', newline="") as myfile:
    combinations = [[y,x] for y in list_Lat for x in list_Long]
    wr = csv.writer(myfile)
    wr.writerows(combinations)

关于python - 在 python 中使用 X 和 Y 值从两个 CSV 文件创建单个 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50990042/

相关文章:

python - 将 numpy 数组作为 Pandas 中的列保存/加载到 csv 文件

java - 使用 jOOQ 从 CSV 导入日期

python - 使用增量运算符时,numpy 数组之和在数据类型方面的不同行为

python - 在私有(private)网站上托管交互式 jupyter notebook

python - python unittest 中的变量范围

Python 字典——为什么 keys()、values() 返回列表而不是集合?

html - 如何将 HTML 表格转换为 csv 格式?

python - 将元素加在一起直到某个索引

java - 重构依赖循环的方法

c - 按降序显示列表