gpt4 book ai didi

Python 正在处理数据?

转载 作者:搜寻专家 更新时间:2023-10-30 19:55:36 25 4
gpt4 key购买 nike

该程序的基础是将邮政编码(英国版邮政编码)转换为坐标。所以我有一个包含大量邮政编码(以及其他附加数据,如房价)的文件和另一个包含所有英国邮政编码及其相关坐标的文件。

我将这两个文件都转换成列表,然后在 for 循环中使用 for 循环来遍历并比较任一文件中的邮政编码。如果文件 1 中的邮政编码 == 文件 2 中的邮政编码,则获取坐标并将其附加到相关文件。

我的代码也已按照我的需要启动并运行。我所有的测试都输出了我想要的结果,这很棒。

唯一的问题是它只能处理小批量数据(我一直在测试包含约 100 行的 .csv 文件 - 创建 100 个内部列表的列表)。

现在我想将我的程序应用于我的整个数据集。我运行了一次,没有任何反应。我走开了,看了会儿电视,还是什么也没发生。 IDLE 不会让我退出程序或任何东西。所以我重新启动并再次尝试,这次添加了一个计数器来查看我的代码是否正在运行。我运行代码,计数器开始运行。直到达到 78902,即我的数据集的大小。然后它停止并且什么都不做。我什么也做不了,也无法关闭窗口。

烦人的是,它甚至无法读取 CSV 文件,所以我无法操作我的数据。

这是卡住的代码(代码的第一部分):

    #empty variable to put the list into    
lst = []
# List function enables use for all files
def create_list():

#find the file
file2 = input('enter filepath:')
#read the file and iterate over it to append into the list
with open(file2, 'r') as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
lst.append(row)
return lst

那么有人知道让我的数据更易于管理的方法吗?

编辑:对于那些感兴趣的人,这里是我的完整代码:

from tkinter.filedialog import asksaveasfile
import csv

new_file = asksaveasfile()

lst = []
# List function enables use for all files
def create_list():
#empty variable to put the list into
#find the file
file2 = input('enter filepath:')
#read the file and iterate over it to append into the list
with open(file2, 'r') as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
lst.append(row)
return lst


def remove_space(lst):
'''(lst)->lst
Returns the postcode value without any whitespace

>>> ac45 6nh
ac456nh
The above would occur inside a list inside a list
'''
filetype = input('Is this a sale or crime?: ')
num = 0
#check the filetype to find the position of the postcodes
if filetype == 'sale':
num = 3
#iterate over the postcode to add all characters but the space
for line in range(len(lst)):
pc = ''
for char in lst[line][num]:
if char != ' ':
pc = pc+char
lst[line][num] = pc

def write_new_file(lst, new_file):
'''(lst)->.CSV file
Takes a list and writes it into a .CSV file.
'''
writer = csv.writer(new_file, delimiter=',')
writer.writerows(lst)
new_file.close()


#conversion function
def find_coord(postcode):

lst = create_list()
#create python list for conversion comparison
print(lst[0])
#empty variables
long = 0
lat = 0
#iterate over the list of postcodes, when the right postcode is found,
# return the co-ordinates.
for row in lst:
if row[1] == postcode:
long = row[2]
lat = row[3]
return str(long)+' '+str(lat)

def find_all_coord(postcode, file):

#empty variables
long = 0
lat = 0
#iterate over the list of postcodes, when the right postcode is found,
# return the co-ordinates.
for row in file:
if row[1] == postcode:
long = row[2]
lat = row[3]
return str(long)+' '+str(lat)

def convert_postcodes():
'''
take a list of lst = []
#find the file
file2 = input('enter filepath:')
#read the file and iterate over it to append into the list
with open(file2, 'r') as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
lst.append(row)
'''
#save the files into lists so that they can be used
postcodes = []
with open(input('enter postcode key filepath:'), 'r') as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
postcodes.append(row)
print('enter filepath to be converted:')
file = []
with open(input('enter filepath to be converted:'), 'r') as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
file.append(row)
#here is the conversion code
long = 0
lat = 0
matches = 0
for row in range(len(file)):
for line in range(len(postcodes)):
if file[row][3] == postcodes[line][1]:
long = postcodes[line][2]
lat = postcodes[line][3]
file[row].append(str(long)+','+str(lat))
matches = matches+1
print(matches)
final_file = asksaveasfile()
write_new_file(file, final_file)

我从 IDLE 中单独调用这些函数,这样我就可以在让程序自行运行它们之前对其进行测试。

最佳答案

您的问题是查找所有文件中的所有代码,这会进行大量比较。

您可以尝试将其保存在字典中,邮政编码是关键。

关于Python 正在处理数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19103493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com