让我们说我有一个像这样的Python字符串:
Some first text: 2342
Another line here: 284
Maybe third line: 458
And forth line: 199
我想从这个字符串创建4个整数变量,使得var =(第1行中的数字)。并且var2 =(第2行中的数字)..依此类推。
怎么可能?
答案 0 :(得分:2)
您可以将所有数字存储在数组中:
application-logger.xml
输出:
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/application.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- Daily rollover with compression -->
<fileNamePattern>logs/application-log-%d{yyyy-MM-dd}.gz</fileNamePattern>
<!-- keep 30 days worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date{yyyy-MM-dd HH:mm:ss ZZZZ} - [%level] - from %logger in %thread %n%message%n%xException%n
</pattern>
</encoder>
</appender>
<appender name="FILE-ASYNC" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="FILE"/>
<queueSize>1024</queueSize>
<discardingThreshold>0</discardingThreshold>
</appender>
<appender name="LOGSTASH" class="com.cwbase.logback.RedisAppender">
<source>play-core</source>
<host>[HIDDEN]</host>
<port>6379</port>
<tags>prod</tags>
<key>logstash</key>
</appender>
<appender name="LOGSTASH-ASYNC" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="LOGSTASH"/>
<queueSize>1024</queueSize>
<discardingThreshold>0</discardingThreshold>
</appender>
<appender name="SENTRY" class="com.getsentry.raven.logback.SentryAppender">
<dsn>[HIDDEN]/dsn>
<tags>env:production</tags>
</appender>
<appender name="SENTRY-ASYNC" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="SENTRY"/>
<queueSize>1024</queueSize>
<discardingThreshold>0</discardingThreshold>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<appender name="ACCESS_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${user.dir}/web/logs/access.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>access-log-%d{yyyy-MM-dd}.gz</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date{yyyy-MM-dd HH:mm:ss ZZZZ} %message%n</pattern>
<immediateFlush>false</immediateFlush>
</encoder>
</appender>
<logger name="access" level="INFO" additivity="false">
<appender-ref ref="ACCESS_FILE"/>
</logger>
<logger name="be.objectify.deadbolt.java.actions.AbstractDeadboltAction" level="OFF"/>
<logger name="application" level="WARN"/>
<logger name="play" level="WARN"/>
<root level="ERROR">
<appender-ref ref="LOGSTASH-ASYNC"/>
<appender-ref ref="FILE-ASYNC"/>
<appender-ref ref="SENTRY-ASYNC"/>
</root>
</configuration>
然后数组的第一个元素将对应于第一行等
答案 1 :(得分:0)
试试这个:
words = somestring.split()
for i in range(len(words)):
try:
locals()['int_%s'%(i)] = int(words[i])
exept:
pass
答案 2 :(得分:0)
您可以使用for
循环和is_digit
方法制作。
# let's guess input text is called TEXT.
TEXT = '''\
Some first text: 2342
Another line here: 284
Maybe third line: 458
And forth line: 199'''
# we can make like this:
splited_TEXT = TEXT.split('\n')
# make new(wanted) dictionary
new_dict = {}
# then use for loop:
for num, sentence in enumerate(splited_TEXT):
for word in sentence.split(' '): # split by space
if word.is_digit: # check if word is digit
new_dict[num] = word
continue
print(new_dict)
然后你可以查看{0:2342,1:284 ......}
答案 3 :(得分:0)
我发现这条简单的线条有效:
[int(s) for s in TEXT.split() if s.isdigit()]
对于上面的TEXT。