从ASCII文件名中拉取数字不起作用 [英] Pulling numbers from ASCII filename not working

查看:73
本文介绍了从ASCII文件名中拉取数字不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了档案但却找不到其他人这个

的问题。基本上我正在抓取目录中的所有ASCII文件,并且

对它们进行地理处理。我需要计算一个z因子,它基于

正在处理的ASCII文件的纬度,它在

文件名中。如果我手动输入代码,它会工作并从ASCII文件名中读取

纬度值,但在ArcGIS中运行时,它会在到达int(LatString)时崩溃。 Isnumber()返回false为

纬度。从ASCII文件名中读取值是否有什么不同?


import sys,os,win32com.client,string,gc


#获取工作空间中ASCII转换为栅格的ASCII文件列表

转换

filenames = os.listdir(gp.workspace)

filenames = [filename.lower()
文件名中的文件名


if(filename [-4:]。lower()=="。 asc"和filename [0]!=" - ")]

文件名中的文件名:


#对于每个ASCII文件,创建Hillshade。

#通过使用弧度计算Z单位来计算纬度

纬度=文件名[1:3]

LatString = str(纬度)

LatInt = int(LatString)

radians = LatInt * 0.0174532925

zFactor = 1 /(113200 *(cos(弧度)))

解决方案

首先: http://www.catb.org/~esr/faqs/smart-questions.html


你说你的程序''崩溃''到达'' 'INT(LatString)'。我是猜测(这完全是一种猜测,因为你不告诉我们)你是否已经得到了像''ValueError这样的异常:int()的无效文字:

无论''。我再次猜测''LatString''并不包含你认为它在这一点上做什么。您是否尝试插入打印

语句以显示LatString包含的内容?


至少有一个

os.listdir返回的文件名(或目录)在你看的地方没有整数值。

请记住os.listdir返回子目录以及文件。

你可能想看看使用glob.glob()而是将它限制为

到* .asc。


其次,


filenames = [filename.lower()\

文件名中的文件名\

if(filename [-4:]。lower()==" .asc" and filename [0]!=" - ")]


最好改写为(未经测试) :


导入glob

filenames = glob.glob(os.path.join(gp.workspace,''* .asc''))

filenames = [f.lower()for f in filenames if if f.startswith('' - '')]


Larry Bates
< br $>
IamIan写道:我搜索了档案,但找不到其他有这个问题的人。基本上我正在抓取目录中的所有ASCII文件并对它们进行地理处理。我需要根据正在处理的ASCII文件的纬度来计算z因子,该文件位于
文件名中。如果我手动输入代码,它会工作并从ASCII文件名中读取
纬度值,但在ArcGIS中运行时,它会在到达int(LatString)时崩溃。 Isnumber()也为纬度返回false。从ASCII文件名读取值有什么不同吗?

import sys,os,win32com.client,string,gc

#获取ASCII列表工作区中的文件,用于ASCII转换器
转换
filenames = os.listdir(gp.workspace)
filenames = [filename.lower()
用于文件名中的文件名 if(filename [-4:]。lower()==" .asc" and filename [0]!=" - ")]
对于文件名中的文件名:
#对于每个ASCII文件,创建山体阴影。
#通过使用弧度计算Z单位来计算纬度
纬度=文件名[1:3]
LatString = str(纬度)
LatInt = int(LatString)
radians = LatInt * 0.0174532925
zFactor = 1 /(113200 *(cos(弧度)))



< blockquote>我得到的异常是TypeError:无法为字符串添加值''int''。我已经查看了LatString,它是

纬度('''''等等的字符串表示。当我包含LatInt = int(LatString)时,但是当我尝试打印

LatInt'的值时,奇怪的是异常被提出不是
将它乘以另一个数字。


文件名是N16W110.asc的行。 - 还有另一种方法

将LatString转换成一个数字用于乘法目的吗?


I searched the archives but couldn''t find anyone else with this
problem. Basically I''m grabbing all ASCII files in a directory and
doing geoprocessing on them. I need to calculate a z-factor based on
the latitude of the ASCII file being worked on, which is in the
filename. If I type in the code manually it works and reads the
latitude value from the ASCII filename, but when run within ArcGIS it
crashes when it gets to int(LatString). Isnumber() returned false for
Latitude as well. Is there something different about reading values
from an ASCII filename?

import sys, os, win32com.client, string, gc

# Get a list of ASCII files in the workspace for ASCII To Raster
conversion
filenames = os.listdir(gp.workspace)
filenames = [filename.lower()
for filename in filenames
if (filename[-4:].lower() == ".asc" and filename[0] != "-" )]
for filename in filenames:

# For each ASCII file, create Hillshade.
# account for latitude by computing Z units using radians
Latitude = filename[1:3]
LatString = str(Latitude)
LatInt = int(LatString)
radians = LatInt * 0.0174532925
zFactor = 1/(113200 * (cos(radians)))

解决方案

First: http://www.catb.org/~esr/faqs/smart-questions.html

You say your program ''crashes'' when it gets to ''int(LatString)''. I''m
guessing (and it''s entirely a guess, since you don''t tell us) that you
are getting an exception like ''ValueError: invalid literal for int():
whatever''. I''m guessing again that ''LatString'' doesn''t contain what
you think it does at this point. Have you tried inserting a print
statement to show you just what ''LatString'' contains?


At lest one of the filenames (or directories) being returned by
os.listdir doesn''t have integer value where you are looking.
Remember that os.listdir returns subdirectories as well as files.
You may want to look at using glob.glob() instead and limit it
to *.asc.

Secondly,

filenames = [filename.lower() \
for filename in filenames \
if (filename[-4:].lower() == ".asc" and filename[0] != "-" )]

is better rewritten as (not tested):

import glob
filenames=glob.glob(os.path.join(gp.workspace, ''*.asc''))
filenames = [f.lower() for f in filenames if not f.startswith(''-'')]

Larry Bates

IamIan wrote:

I searched the archives but couldn''t find anyone else with this
problem. Basically I''m grabbing all ASCII files in a directory and
doing geoprocessing on them. I need to calculate a z-factor based on
the latitude of the ASCII file being worked on, which is in the
filename. If I type in the code manually it works and reads the
latitude value from the ASCII filename, but when run within ArcGIS it
crashes when it gets to int(LatString). Isnumber() returned false for
Latitude as well. Is there something different about reading values
from an ASCII filename?

import sys, os, win32com.client, string, gc

# Get a list of ASCII files in the workspace for ASCII To Raster
conversion
filenames = os.listdir(gp.workspace)
filenames = [filename.lower()
for filename in filenames
if (filename[-4:].lower() == ".asc" and filename[0] != "-" )]
for filename in filenames:

# For each ASCII file, create Hillshade.
# account for latitude by computing Z units using radians
Latitude = filename[1:3]
LatString = str(Latitude)
LatInt = int(LatString)
radians = LatInt * 0.0174532925
zFactor = 1/(113200 * (cos(radians)))



The exception I get is "TypeError: Cannot add value ''int'' to string." I
have looked at LatString, and it is the string representation of
latitude (''17'' etc.). What''s odd is that the exception is raised not
when I include LatInt = int(LatString), but when I try to print
LatInt''s value or multiply it by another number.

Filenames are along the lines of "N16W110.asc" - is there another way
to get LatString into a number for multiplication purposes?


这篇关于从ASCII文件名中拉取数字不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆