小编Joh*_*web的帖子

在Python中以单行打印

鉴于一些代码:

keyword=re.findall(r'ke\w+ = \S+',s)
score=re.findall(r'sc\w+ = \S+',s)
print '%s,%s' %(keyword,score)
Run Code Online (Sandbox Code Playgroud)

上面代码的输出是:

['keyword = NORTH','keyword = GUESS','keyword = DRESSES','keyword = RALPH','keyword = MATERIAL'],['得分= 88466','得分= 83965','得分= 79379' ,'得分= 74897','得分= 68168']

但我希望格式应该是不同的行:

NORTH,88466
GUESS,83935
DRESSES,83935
RALPH,73379
MATERIAL,68168
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
241
查看次数

Python中的Strptime错误

我一直在撞墙而出现以下错误:

time
Traceback (most recent call last):
  File "csvtest.py", line 37, in <module>
    date = time.strptime(datestring, "%Y-%m-%d %H:%M:%S")
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 454, in _strptime_time
    return _strptime(data_string, format)[0]
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 325, in _strptime
    (data_string, format))
ValueError: time data 'time' does not match format '%Y-%m-%d %H:%M:%S'
Run Code Online (Sandbox Code Playgroud)

输入是来自具有格式的文件的一行 - 年有意为垃圾数据:

3354-03-16 15:30:00
3354-03-16 16:00:00
3354-03-16 16:30:00
3354-03-16 16:30:00
Run Code Online (Sandbox Code Playgroud)

我正在使用的代码如下:


import sys
import csv
from datetime import datetime
import time

filename = open('data.csv', 'rb')


spam = csv.reader(filename, delimiter=',')
for row in spam:

    datestring …
Run Code Online (Sandbox Code Playgroud)

python datetime

0
推荐指数
1
解决办法
3886
查看次数

C中的数组指针有问题

我正在解决去年的GATE问题论文,我坚持这个问题

以下C程序片段打印什么?

char c[]="GATE2011";
char *p =c;
printf ("%s", p+p[3]-p[1]);
Run Code Online (Sandbox Code Playgroud)

答案是'2011'

我知道在c中,数组变量是指向数组第一个地址的指针.我的逻辑答案是"E2011",但输出是2011年

有人可以解释这涉及的指针数学吗?

c arrays pointers

0
推荐指数
1
解决办法
129
查看次数

获取响应代码= 406.响应消息=解析JSON时不可接受的错误

我使用ActiveResource构建了一个由另一个(Rails也称为)调用的Rails应用程序.

情况是我将第一个应用程序中的信息公开为JSON,如下所示:

应用1:

class PromotionsController < ApplicationController

  # GET /promotions
  # GET /promotions.xml
  def index
    @promotions = Promotion.all

    respond_to do |format|
      format.json  { render :json => @promotions }
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我通过ActiveResource模型在App 2上收到它,如下所示:

class Promotion < ActiveResource::Base
  self.site = "app_1_url"
  self.element_name = "promotion"
end
Run Code Online (Sandbox Code Playgroud)

当我想将数据作为JSON读取时,执行以下操作,我得到406 Not Acceptable错误消息:

class PromotionsController < ApplicationController
  # GET /promotions
  # GET /promotions.xml
  def index
    @promotions = Promotion.all

    respond_to do |format|
      format.json { render :json => @promotions }
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试将信息解析为XML(与上面显示的代码相同时,除了将"json"改为"xml"之外)它的工作原理.

有任何想法吗?

谢谢.

json ruby-on-rails-3

0
推荐指数
1
解决办法
2663
查看次数

Python:线程不会启动

我有一个继承自threading.Thread. 由于某种原因,线程不想启动。

这是我的代码:

import time,threading,re,socket


class PyWatch(threading.Thread):

    filename = ""


    def __init__(self,filename):
        threading.Thread.__init__(self)
        print "initiating..."
        self.filename = filename


     def run(self):
        print "running..."
        thefile = open (self.filename)
        thefile.seek(0,2)      # Go to the end of the file
        while True:
                line = thefile.readline()
                if not line:
                     time.sleep(0.1)    # Sleep briefly
                     continue
                yield line
                self.process(line)


    def process(self,line):
        ip =  self.filterIPFromLine(line)
        print ip                

    def filterIPFromLine(self,line):
        ip = None
        if '/var/ossec/active-response/bin/firewall-drop.sh' in  str( line ).lower():
            ip = re.match( "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])" )

            try:
                socket.inet_aton(ip[0])
                ip = …
Run Code Online (Sandbox Code Playgroud)

python multithreading

0
推荐指数
1
解决办法
3163
查看次数

以相反顺序打印输入字符串字

使用if while/do- while,我的工作是以相反的顺序打印以下用户的输入(字符串值).

例如:

输入字符串值:"你是美国人"以相反的顺序输出:"American are You"

有没有办法做到这一点?

我试过了

string a;
cout << "enter a string: ";
getline(cin, a);
a = string ( a.rbegin(), a.rend() );
cout << a << endl;
return 0;
Run Code Online (Sandbox Code Playgroud)

...但这会颠倒单词拼写的顺序,而拼写不是我想要的.

我也应该加入ifwhile声明,但不知道如何.

c++ string reverse

0
推荐指数
1
解决办法
2万
查看次数

使用sed填充空的CSV字段?

我需要在文件中的所有空单元格(没有任何值)中输入一些值(例如 1).如何使用完成?

如果使用无法实现这一点,那么使用是不可能的?

csv perl sed

0
推荐指数
1
解决办法
462
查看次数

用于重复下一个字符的Vim扫描线; 不工作

我能够扫描线与下一个字符f{char},并重复;或反向用,.

但是当我使用t{char}它时,它不起作用f{char}.它找到第一个但不能重复它;.

为什么是这样?


更新

我的 .vimrc

execute pathogen#infect()

syntax on
"filetype plugin indent on

set expandtab
set shiftwidth=2
set softtabstop=2

colorscheme darkblue

set cursorline
hi Comment ctermfg=103
hi CursorLine term=none cterm=none ctermbg=17 guibg=236

set hlsearch
set number
set nowrap
set omnifunc=pythoncomplete#Complete
set omnifunc=javascriptcomplete#CompleteJS
set omnifunc=htmlcomplete#CompleteTags
set omnifunc=csscomplete#CompleteCSS
set omnifunc=xmlcomplete#CompleteTags
set omnifunc=phpcomplete#CompletePHP
set cmdheight=1
set laststatus=2
set statusline=%<%F\ %m%r%h%w%{'['.(&fenc!=''?&fenc:&enc).']['.&ff.']'}%=%l,\ [TYPE=%Y]\ [ASCII=\%03.3b]\ [HEX=\%02.2B]\ %c%V%8P

nmap <C-l> <C-l>:nohlsearch<CR> …
Run Code Online (Sandbox Code Playgroud)

vim

0
推荐指数
1
解决办法
158
查看次数

如何清空std :: string?

我的代码的这一部分在一个无限循环中运行并显示一个数字,但我需要在使用后清空该字符串,因为它在循环中运行,它保持相乘.(对不起我的英文,这不是我的母语)

text ---> text text ----> text text text

cartemap << "Carte: " << currentmap;  //cartemap is a std:string currentmap a integer'

MESSAGE1 = TTF_RenderText_Solid( font,  cartemap.str().c_str() , noir );

apply_surface( 70, 70, MESSAGE1, SCREEN );

SDL_FreeSurface(MESSAGE1);
Run Code Online (Sandbox Code Playgroud)

c++ iostream

0
推荐指数
1
解决办法
228
查看次数

C++家庭作业帮助(绘制一个包括/ \的棒屋)

我正在参加C++大学课程的介绍,我很难接受一个非常简单的任务.我必须绘制房屋形状,屋顶必须包含"/"和"\".编译运行后:这样的事情

cout <<"  /\ " <<endl;
cout <<" /  \ " <<endl;
Run Code Online (Sandbox Code Playgroud)

它出现了缺少"/",它看起来像这样

   /
  /
 /
 ====== 
Run Code Online (Sandbox Code Playgroud)

请指教,我的教科书没有信息,我可以找到任何参考资源,包括我的Bloodshed DevC++ 5.7.1上的帮助功能.

c++

0
推荐指数
1
解决办法
572
查看次数

标签 统计

c++ ×3

python ×3

arrays ×1

c ×1

csv ×1

datetime ×1

iostream ×1

json ×1

multithreading ×1

perl ×1

pointers ×1

reverse ×1

ruby-on-rails-3 ×1

sed ×1

string ×1

vim ×1