将包含屏幕抓取值的列添加到 CSV 文件

发布于 2024-10-30 14:24:34 字数 896 浏览 0 评论 0原文

我正在制作德克萨斯州埃尔帕索颁发的建筑许可证地图。这是我第一次涉足 python 的奇妙世界,这很有趣,但我无法找出 csv 模块来拯救我的生命。

我有一个 .csv 文件,其中包含字段“日期”、“建筑编号”、“建筑类型”、“描述”、“项目名称”、“到期日期”、“状态”,我需要向其中添加一个名为“地址”与每个建筑物号码相关的地址。

我已经获得了使用建筑许可证号获取地址的代码,但我不知道如何按顺序获取每个建筑号,使用它来获取许可证的地址,然后将其写入新的'地址”栏。我无法理解为什么他们不能首先将地址放入 .csv 文件中。

到目前为止,我已经得到了地址:

from BeautifulSoup import BeautifulSoup
import urllib

CaseNo = "RSP11-00459"    # this is an example of the building permit number format
DBaseEntry = urllib.urlopen("http://permits.elpasotexas.gov/tm_bin/tmw_cmd.pl?tmw_cmd=StatusViewCase&shl_caseno=%s " % CaseNo)
Soup = BeautifulSoup(DBaseEntry.read())
Dang = Soup.findAll("td", { "class" : "ReportCell"})[5]
Address = str(Dang)
TableEntry = Address[23:-5].strip( )
print TableEntry

它不太优雅,但我需要一种方法来获取 CaseNo 形式的建筑物编号值,然后将 TableEntry 写入(新创建的)Address 列的值。 如果重要的话,这些表每个都有 100 行。

有什么想法/建议吗?

I am working on building a map of building permits issued in El Paso, TX. This is my first foray into the wonderful world of python, and it's been fun, but I cannot figure out the csv module to save my life.

I have a .csv file containing the fields 'Date', 'Building Number', 'Building Type', 'Description', 'Project Name', 'Expiration Date', 'Status', to which I need to add a column called "address" with the address associated with each building number.

I have got the code to get the address using the building permit number down, but I am at a loss as to how to get each building number in sequence, use it to get the address for the permit, then write it to the new 'address' column. why they couldn't have just put the address in the .csv file in the first place is beyond me.

here's what I've got so far to get the address:

from BeautifulSoup import BeautifulSoup
import urllib

CaseNo = "RSP11-00459"    # this is an example of the building permit number format
DBaseEntry = urllib.urlopen("http://permits.elpasotexas.gov/tm_bin/tmw_cmd.pl?tmw_cmd=StatusViewCase&shl_caseno=%s " % CaseNo)
Soup = BeautifulSoup(DBaseEntry.read())
Dang = Soup.findAll("td", { "class" : "ReportCell"})[5]
Address = str(Dang)
TableEntry = Address[23:-5].strip( )
print TableEntry

it's inelegant, but I need a way to get the building number values as CaseNo, then write TableEntry as the value for the (newly created) Address column.
these tables will all have 100 rows each, if it matters.

any thoughts/suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

﹏半生如梦愿梦如真 2024-11-06 14:24:34

不太确定我完全理解你的问题,但如果它是关于使用 CSV 模块,那么你的代码应该如下所示:

import csv,os

with open('foo.csv', 'rb') as r, open('foo.tmp', 'wb') as w:
    reader = csv.reader(r)
    writer = csv.writer(w)

    for row in reader:
        newrow=whatever_processing_you_need(row)
        writer.writerow(newrow)

os.unlink('foo.csv')
os.rename('foo.tmp','foo.csv')

Not really sure i fully understand your question, but if it's about using the CSV module, then your code should look something like that:

import csv,os

with open('foo.csv', 'rb') as r, open('foo.tmp', 'wb') as w:
    reader = csv.reader(r)
    writer = csv.writer(w)

    for row in reader:
        newrow=whatever_processing_you_need(row)
        writer.writerow(newrow)

os.unlink('foo.csv')
os.rename('foo.tmp','foo.csv')
七秒鱼° 2024-11-06 14:24:34

据我了解,您使用报告的代码获得了地址,现在您想将此地址附加到您的 csv 文件中。如果是这样,我会使用@bdev解决方案,并且“whatever_processing_you_need”将简单地转换为:“找到您检索到的地址的相应行并将最后一列附加到该地址”。

As far as I understand, you got the address using the code you reported and now you want to append this address to your csv file. If it is so, I'd use @bdev solution, and "whatever_processing_you_need" will simply translate into: "find the corresponding row of the address you retrieved and append the last column with the address".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文