如何从python中的文件中提取必要的内容?

时间:2015-10-19 08:00:35

标签: python file file-read

我有一个私钥文件(private.key),其内容采用以下格式

select *,time_to_sec(timediff(date_note1, date_note2)) / 3600 from (SELECT CASE
    WHEN DATE_FORMAT(date,'%H:%i:%s')  < '8:00:00' THEN CONCAT(date, ' 8:00:00')
    ELSE date
    END AS date_note1 ,

    CASE
    WHEN DATE_FORMAT(date2,'%H:%i:%s')  > '17:00:00' THEN CONCAT(date2, ' 17:00:00')
    ELSE date2
    END AS date_note2

    FROM `test` where WEEKDAY(date) !=5 and WEEKDAY(date) !=6) as tbl

我正在阅读文件如下:

-----BEGIN PRIVATE KEY-----
blahblahblahblahblahblahblahblah
blahblahblahblahblahblahblahblah
blahblahblahblahblahblahblahblah
-----END PRIVATE KEY-----

如何消除第一行和最后一行并仅将私钥内容提取到变量pk中?

在python中期待类似的东西

pk = open( 'private.key', 'rb' ).read()

4 个答案:

答案 0 :(得分:3)

with open('private.key', 'r') as f: # use with here, it will auto close the file.
    pk = f.readlines()

pk = pk[1:-1]

现在,pk是一个按行保存私钥的列表 要打印出来,您可以像这样使用print()

for i in pk:
    print(pk)

或者,就像egrep一样:

with open('private.key', 'r') as f: # use with here, it will auto close the file.
    pk = f.read()

for i in pk:
    if 'PRIVATE' not in i:
        print(i)

答案 1 :(得分:1)

你可以使用

pk = open( 'file.txt', 'r' ).readlines() #or 'rb' if there's a need

if len(pk) > 2:
    for line in pk[1:-1]:
        print line,
        #or do anything you like to do with line

避免处理行列表中的第一行和最后一行

答案 2 :(得分:1)

对于允许文件中的注释(BEGIN PRIVATE KEYEND PRIVATE KEY保护线之外的任何内容)的更通用的解决方案,并且还处理文件中的多个私钥(但允许您尽快停止阅读如果您找到了第一个,那么您可以在生成器中使用非常简单的状态机算法:

def private_keys(file):
    key = []
    inside_guards = False
    for line in file:
        if line == "-----BEGIN PRIVATE KEY-----":
            inside_guards = True
            continue
        if line == "-----END PRIVATE KEY-----":
            inside_guards = False
            yield data
            data = []
            continue

        if inside_guards:
            data.append(line)

答案 3 :(得分:0)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.root5solutions.mirrealtors.GalleryFragment">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <RelativeLayout
            android:id="@+id/main_layout"
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:background="?attr/colorPrimary"
                android:elevation="6dp"
                android:minHeight="?attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

            <android.support.design.widget.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/toolbar"
                android:background="?attr/colorPrimary"
                android:elevation="6dp"
                android:minHeight="?attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_below="@id/tab_layout"/>

        </RelativeLayout>
    </RelativeLayout>



</FrameLayout>