小编Mar*_*kus的帖子

理解一些Python代码

我正在使用imaplib进行Gmail访问,并遇到:

# Count the unread emails
status, response = imap_server.status('INBOX', "(UNSEEN)")
unreadcount = int(response[0].split()[2].strip(').,]'))
print unreadcount
Run Code Online (Sandbox Code Playgroud)

我只想知道:

status,
Run Code Online (Sandbox Code Playgroud)

在"response ="之前做.我会谷歌它,但我不知道我甚至要求找到答案:(.

谢谢.

python

5
推荐指数
2
解决办法
156
查看次数

如何从资源字符串中获取字符串到片段中

我尝试阅读 Stack Overflow 上的许多解决方案,发现它们要么不适用于我的场景,要么我根本不明白他们的解释(我对 Java 和 Android 非常陌生。我在 res/values 下设置了字符串我希望在类中使用的 /strings.xml:-

public class AttractionFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.word_list, container, false);

    // create an array list of details
    final ArrayList<Details> details = new ArrayList<>();

    // Details details
    details.add(new Details(getActivity().getString(R.string.fun_bigsplash_name), getString(R.string.fun_bigsplash_addr), R.string.fun_bigsplash_num, R.drawable.bigsplash));
Run Code Online (Sandbox Code Playgroud)

我已经尝试了许多变体(它们不同的原因只是为了展示我尝试过的东西)但无法解决。R.drawable.bigsplash 工作正常(当我为其他人使用文字字符串时)。

错误消息指出一个 int,我认为这意味着它正在获取引用而不是实际字符串。

如何从片段中获取字符串?

谢谢。

android

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

NetworkSecurityConfig:未使用平台默认值指定网络安全配置

我正在尝试连接到这些地方之一并获取JSON数据:-

https://content.guardianapis.com/search?q=debate&tag=politics/politics&from-date=2014-01-01&api-key=test
https://content.guardianapis.com/search?q=debates&api-key=test
https://content.guardianapis.com/search?api-key=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx
Run Code Online (Sandbox Code Playgroud)

我可以通过浏览器访问所有这三个,但是当我尝试通过Android应用访问它们时,出现以下错误:

NetworkSecurityConfig: No Network Security Config specified, using platform default
Error response code: 400
Run Code Online (Sandbox Code Playgroud)

我已将此添加到manifest.xml:-

<uses-permission android:name="android.permission.INTERNET"/>
Run Code Online (Sandbox Code Playgroud)

我还将此添加到manifest.xml:

android:networkSecurityConfig="@xml/network_security_config"
Run Code Online (Sandbox Code Playgroud)

并创建了res / xml / network_security_config.xml,其中包含:-

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config>
        <domain includeSubdomains="true">content.guardianapis.com</domain>>
    </domain-config>
Run Code Online (Sandbox Code Playgroud)

将错误更改为:

D/NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: true
Error response code: 400
Run Code Online (Sandbox Code Playgroud)

我知道它不见了:

<trust-anchors>
    <certificates src="@raw/my_ca"/>
</trust-anchors>
Run Code Online (Sandbox Code Playgroud)

但我不知道证书在哪里或在什么地方,是否需要它。

不太确定发生了什么,将不胜感激。

另外:-我可以很好地连接并从此URL获取JSON:-

https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&eventtype=earthquake&orderby=time&minmag=6&limit=10
Run Code Online (Sandbox Code Playgroud)

我所得到的是:

No Network Security Config specified, using platform default
Run Code Online (Sandbox Code Playgroud)

购买不是错误400,而是获得200。因此,这让我觉得有些奇怪的事情正在发生,但不确定在哪里。

java networking android asp.net-web-api

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

使用python ElementTree解析未知元素的xml文件

我希望从多用途xml文件中提取所有标记名称及其相应的数据.然后将该信息保存到python字典中(例如tag = key,data = value).标签名称和值的标记是未知的,数量未知.

    <some_root_name>
        <tag_x>bubbles</tag_x>
        <tag_y>car</tag_y>
        <tag...>42</tag...>
    </some_root_name>
Run Code Online (Sandbox Code Playgroud)

我正在使用ElementTree并且可以成功提取根标记,并且可以通过引用标记名称来提取值,但是无法在不引用标记名称的情况下找到简单地迭代标记和数据的方法.

任何帮助都会很棒.

谢谢.

python elementtree xml-parsing

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

使用imaplib使用文件中的凭据登录Gmail

我提前道歉,我知道这将是一个简单的答案,但我没有任何运气,它开始让我烦恼.

我只想使用imaplib和从文件中提取的凭据登录我的Gmail帐户.我可以登录Gmail而不用担心:

srv = imaplib.IMAP_SSL("imap.gmail.com")
srv.login("bob@gmail.com", "mypassword")
Run Code Online (Sandbox Code Playgroud)

但相反,我正在尝试使用文本文件(credentials.dat).文件内部有2行:

bob@gmail.com
mypassword
Run Code Online (Sandbox Code Playgroud)

我的代码如下:

import imaplib
CREDS = open("credentials.dat", "r")

account = CREDS.readline()
password = CREDS.readline()

srv = imaplib.IMAP4_SSL("imap.gmail.com")
srv.login(account, password)
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我得到了以下错误:

Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "/usr/lib64/python2.4/imaplib.py", line 480, in login
       typ, dat = self._simple_command('LOGIN', user, self._quote(password))
    File "/usr/lib64/python2.4/imaplib.py", line 1028, in _simple_command
       return self._command_complete(name, self._command(name, *args))
    File "/usr/lib64/python2.4/imaplib.py", line 865, in _command_complete
       raise self.error('%s command error: %s %s' % (name, typ, data))
       imaplib.error: LOGIN …
Run Code Online (Sandbox Code Playgroud)

python

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

在 Python IO 错误中解析 XML

我知道这个问题已经在堆栈上被多次询问(和回答)。我已经尝试了很多,但由于某种原因我无法克服这个错误:

Traceback (most recent call last):
  File "J:\eclipse\workspace_python\TF2_Monitor\src\TF2_Monitor.py", line 23, in <module>
    xmldoc = minidom.parse(sampleXML)
  File "C:\Python27\lib\xml\dom\minidom.py", line 1920, in parse
    return expatbuilder.parse(file)
  File "C:\Python27\lib\xml\dom\expatbuilder.py", line 922, in parse
    fp = open(file, 'rb')
IOError: [Errno 2] No such file or directory: '<?xml version = "1.0" encoding = "utf-8"?>\n<Patients>\n    <Patient>\n               <PatientCharacteristics>\n                   <patientCode>3</patientCode>\n               </PatientCharacteristics>\n    </Patient>\n</Patients>'
Run Code Online (Sandbox Code Playgroud)

这是导致问题的代码位:-

if __name__ == '__main__':
   from xml.dom import minidom

   sampleXML = """<?xml version = "1.0" encoding = "utf-8"?>
   <Patients>
      <Patient>
           <PatientCharacteristics>
               <patientCode>3</patientCode>
           </PatientCharacteristics>
      </Patient>
   </Patients>"""


   xmldoc = …
Run Code Online (Sandbox Code Playgroud)

python xml

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