小编Ste*_*ima的帖子

XPath 仅获取嵌套 HTML 的第一个父级

我是 XPath 的新手。有人可以解释一下如何解决这个问题:

<table>
   <tr>
       <td>
           <table>
                <tr>
                    <td>
                         <table>
                             <tr>
                                 <td>Label</td>
                                 <td>value</td>
                             </tr>
                         </table>
                    </td>
                </tr>
           </table>
       </td>
   </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

我尝试获取<tr>其中包含的Label价值,但它对我不起作用,

这是我的代码:

//td[contains(.,'Label')]/ancestor::tr[1]
Run Code Online (Sandbox Code Playgroud)

期望的结果:

<tr>
   <td>Label</td>
   <td>value</td>
</tr>
Run Code Online (Sandbox Code Playgroud)

有人能帮我吗 ?

xpath

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

我怎样才能摆脱"白色空间"

我正在编写服务器客户端项目.服务器端是用Java开发的,客户端用Python开发的.最近,当我尝试在套接字上发送一个字符串时,服务器获取它的空格.假设我发送:

1:user:password
Run Code Online (Sandbox Code Playgroud)

在服务器端我使用字符串拆分将这一个字符串转换为3,第一个字符串(在本例中为"1")将告诉服务器如何处理其余字符串(用户名和密码).现在的问题是,当我从服务器端的套接字打印出来的内容时,我会得到类似这样的内容:

1 : u s e r : p a s s w o r d 
Run Code Online (Sandbox Code Playgroud)

每个角色之间都有空格.我试图使用String函数replaceAll("\\s",""),replaceAll("\\s+","")但它们似乎没有用.我replaceAll("\\S","k")和大写S一起使用,并意识到它实际上向我展示了:

kkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Run Code Online (Sandbox Code Playgroud)

所以我得出结论,那些"白色空间"并不是真正的白色空间,因为它\\S取代了"任何不是空格的东西(包括字母和数字,以及标点符号等)".

我的问题是:那些空白是什么,我怎么能摆脱它们呢?

提前致谢.对不起,很长的帖子.

python java sockets string whitespace

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

无法使用python将元素列表写入文件

我有元素列表,我想使用python使用print()函数在元素下面写入文件.

Python gui:3.3版

示例代码:

D = {'b': 2, 'c': 3, 'a': 1}
flog_out = open("Logfile.txt","w+") 
for key in sorted(D):
    print(key , '=>', D[key],flog_out)
flog_out.close()
Run Code Online (Sandbox Code Playgroud)

我在IDLE gui中运行时的输出:

a => 1 <_io.TextIOWrapper name='Logfile.txt' mode='w+' encoding='cp1252'>
b => 2 <_io.TextIOWrapper name='Logfile.txt' mode='w+' encoding='cp1252'>
c => 3 <_io.TextIOWrapper name='Logfile.txt' mode='w+' encoding='cp1252'>
Run Code Online (Sandbox Code Playgroud)

我没有看到输出文件中写入任何行.我尝试使用flog_out.write(),看起来我们可以在write()函数中传递一个参数.任何人都可以查看我的代码,并告诉我是否遗漏了一些东西.

python python-3.x

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

Python:使用FOR循环插入字典

我已经搜索了论坛,无法理解我是否可以使用以下构造将新条目插入到我的Python词典中...而不将其转换为列表.

for x in range(3):    
   pupils_dictionary = {}
   new_key =input('Enter new key: ')
   new_age = input('Enter new age: ')
   pupils_dictionary[new_key] = new_age
print(pupils_dictionary)
Run Code Online (Sandbox Code Playgroud)

输出如下:

Enter new key: Tim
Enter new age: 45
Enter new key: Sue
Enter new age: 16
Enter new key: Mary
Enter new age: 15
{'Mary': '15'}
Run Code Online (Sandbox Code Playgroud)

为什么只有玛丽:15进入,其他人都没有?

谢谢/

python dictionary

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

在python中使用多个值从dict键返回一个值

我试图从以下字典返回"runnerName":

{u'marketId': u'1.112422365',
 u'marketName': u'1m Mdn Stks',
 u'runners': [{u'handicap': 0.0,
               u'runnerName': u'La Napoule',
               u'selectionId': 8095372,
               u'sortPriority': 1},
              {u'handicap': 0.0,
               u'runnerName': u'Swivel',
               u'selectionId': 701378,
               u'sortPriority': 2},
              {u'handicap': 0.0,
               u'runnerName': u'Deanos Devil',
               u'selectionId': 8100420,
               u'sortPriority': 3},
              {u'handicap': 0.0,
               u'runnerName': u'Bishan Bedi',
               u'selectionId': 8084336,
               u'sortPriority': 4},
              {u'handicap': 0.0,
               u'runnerName': u'In Seine',
               u'selectionId': 8199415,
               u'sortPriority': 5},
              {u'handicap': 0.0,
               u'runnerName': u'Needs The Run',
               u'selectionId': 8199416,
               u'sortPriority': 6},
              {u'handicap': 0.0,
               u'runnerName': u'Appellez Baileys',
               u'selectionId': 8148513,
               u'sortPriority': 7},
              {u'handicap': 0.0,
               u'runnerName': u'Jessy Mae',
               u'selectionId': 7652545,
               u'sortPriority': 8}, …
Run Code Online (Sandbox Code Playgroud)

python dictionary

-2
推荐指数
1
解决办法
111
查看次数

标签 统计

python ×4

dictionary ×2

java ×1

python-3.x ×1

sockets ×1

string ×1

whitespace ×1

xpath ×1