Python文本文件列表

时间:2018-06-22 10:21:38

标签: python list text split

我正在尝试创建一个脚本,该脚本将查看文本文件并将每个单词作为单独的元素存储在列表中。

当尝试使用.split()时,似乎没有将字符串拆分为单个元素。

package com.example.shakeelmughal.assanislam;

import android.content.ContentValues;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Date;

import static android.content.ContentValues.TAG;
import static java.time.LocalTime.now;

/**
 * Created by Shakeel Mughal on 5/30/2018.
 */

public class DatabaseHelper extends SQLiteOpenHelper {
    public Context context;
    private SQLiteDatabase db2;
    public AssetManager mngr;
    SQLiteDatabase db;
    private Resources mResources;
    private InputStream inputstream = null;
    private final static String Dbname = "NamazCounter.db";
    private final static String Tbname = "DailyWork";
    private final static String Tbname2 = "items";
    private final static String Col_1 = "ID";
    private final static String Col_2 = "Fajar";
    private final static String Col_3 = "Zohr";
    private final static String Col_4 = "Asr";
    private final static String Col_5 = "Magrib";
    private final static String Col_6 = "Isha";


    private final static String NCol_1 = "date_for";
    private final static String NCol_2 = "fajr";
    private final static String NCol_3 = "shurooq";
    private final static String NCol_4 = "dhuhr";
    private final static String NCol_5 = "asr";
    private final static String NCol_6 = "maghrib";
    private final static String NCol_7 = "isha";

    public DatabaseHelper(Context context) throws IOException {
        super(context, Dbname, null, 1);
        this.context = context;
        mResources = context.getResources();

        mngr = context.getAssets();
        db = this.getWritableDatabase();
        db = this.getReadableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //namaz counter
        final String Table1 = "CREATE TABLE "+Tbname+"(ID PRIMARY KEY TEXT, Fajar TEXT, Zohr TEXT, Asr TEXT, Magrib TEXT, Isha TEXT)";
        //for namaz timing
        final String Table2 = "CREATE TABLE "+Tbname2+"(date_for TEXT, fajr TEXT, shurooq TEXT, dhuhr TEXT, asr TEXT, maghrib TEXT,isha TEXT)";

        db.execSQL(Table1);
        db.execSQL(Table2);
        Log.d(TAG, "DataBase Created");

        try {
            readDataToDb(db);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+ Tbname);
        db.execSQL("DROP TABLE IF EXISTS "+ Tbname2);
        onCreate(db);



    }

    public void openDatabase() {

        String dbPath = context.getDatabasePath(Dbname).getPath();
        if (db != null) {
            db.isOpen();
            return;
        }
        db = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
    }

    public void closeDatabase() {
        if (db != null) {
            db.close();
        }
    }

    //Json fun
    private String readJsonDataFromFile() throws IOException {

        StringBuilder builder = new StringBuilder();

        try {
            String jsonDataString = null;

            inputstream = mngr.open("NamazTiming.json");
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(inputstream, "UTF-8"));
            while ((jsonDataString = bufferedReader.readLine()) != null) {
                builder.append(jsonDataString);
            }

        } finally {
            if (inputstream != null) {
                inputstream.close();
            }
        }
        return new String(builder);
    }

    public void readDataToDb(SQLiteDatabase db) throws IOException, JSONException {
        final String date = "date_for";
        final String namaz1 = "fajr";
        final String namaz2 = "shurooq";
        final String namaz3 = "dhuhr";
        final String namaz4 = "asr";
        final String namaz5 = "maghrib";
        final String namaz6 = "isha";

        try {
            String jsonDataString = readJsonDataFromFile();
            JSONArray jsonfileJSONArray = new JSONArray(jsonDataString);
            for (int i = 0; i < jsonfileJSONArray.length(); i++) {
                String dateid;
                String nmz1;
                String nmz2;
                String nmz3;
                String nmz4;
                String nmz5;
                String nmz6;


                JSONObject jsonfileObject = jsonfileJSONArray.getJSONObject(i);

                dateid = jsonfileObject.getString(date);
                nmz1 = jsonfileObject.getString(namaz1);
                nmz2 = jsonfileObject.getString(namaz2);
                nmz3 = jsonfileObject.getString(namaz3);
                nmz4 = jsonfileObject.getString(namaz4);
                nmz5 = jsonfileObject.getString(namaz5);
                nmz6 = jsonfileObject.getString(namaz6);

                ContentValues jsonfilevalues = new ContentValues();
                jsonfilevalues.put(NCol_1, dateid);
                jsonfilevalues.put(NCol_2, nmz1);
                jsonfilevalues.put(NCol_3, nmz2);
                jsonfilevalues.put(NCol_4, nmz3);
                jsonfilevalues.put(NCol_5, nmz4);
                jsonfilevalues.put(NCol_6, nmz5);
                jsonfilevalues.put(NCol_7, nmz6);
                long rowID=  db.insert(Tbname2, null, jsonfilevalues);
                if(rowID>-1){
                    int catid=0;
                    Cursor cursor = db.rawQuery("SELECT "+ NCol_1 + " FROM "+ Tbname2+" where "+ NCol_1 + "='" + now() + "'" , null);
                    cursor.moveToFirst();
                    if (cursor.moveToNext()) {
                        catid = (cursor.getInt(0));
                    }

                    cursor.close();
                    JSONArray jsonitemsarray= (JSONArray) jsonfileObject.get("itemsList");
                    readitem(db,jsonitemsarray);
                }

                Log.d(TAG, "Inserted sucessfully" + jsonfilevalues);
            }
        } catch (JSONException e) {
            Log.e(TAG, e.getMessage(), e);
            e.printStackTrace();
        }

    }


    private void readitem(SQLiteDatabase db,JSONArray jsonfileJSONArray) throws IOException, JSONException {

        final String date1 = "date_for";
        final String namaz1 = "fajr";
        final String namaz2 = "shurooq";
        final String namaz3 = "dhuhr";
        final String namaz4 = "asr";
        final String namaz5 = "maghrib";
        final String namaz6 = "isha";

        try {

            for (int i = 0; i < jsonfileJSONArray.length(); i++) {


                String Did;
                String nmzz1 ;
                String nmzz2 ;
                String nmzz3 ;
                String nmzz4 ;
                String nmzz5 ;
                String nmzz6 ;

                JSONObject jsonfileObject1 = jsonfileJSONArray.getJSONObject(i);


                Did = jsonfileObject1.getString(date1);
                nmzz1 = jsonfileObject1.getString(namaz1);
                nmzz2 = jsonfileObject1.getString(namaz2);
                nmzz3 = jsonfileObject1.getString(namaz3);
                nmzz4 = jsonfileObject1.getString(namaz4);
                nmzz5 = jsonfileObject1.getString(namaz5);
                nmzz6 = jsonfileObject1.getString(namaz6);



                ContentValues jsonfilevalues1 = new ContentValues();

                jsonfilevalues1.put(NCol_1, Did);
                jsonfilevalues1.put(NCol_2, nmzz1);
                jsonfilevalues1.put(NCol_3, nmzz2);
                jsonfilevalues1.put(NCol_4, nmzz3);
                jsonfilevalues1.put(NCol_5, nmzz4);
                jsonfilevalues1.put(NCol_6, nmzz5);
                jsonfilevalues1.put(NCol_7, nmzz6);
                db.insert(Tbname2, null, jsonfilevalues1);

                Log.d(TAG, "Inserted sucessfully" + jsonfilevalues1);
            }
        } catch (JSONException e) {
            Log.e(TAG, e.getMessage(), e);
            e.printStackTrace();
        }

    }

    //inserting for counter
    public boolean InsertData(String d_date, String Fjr, String zhr, String assr, String mgrb, String isa)
    {
        SQLiteDatabase db = this.getReadableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(Col_1,d_date);
        contentValues.put(Col_2,Fjr);
        contentValues.put(Col_3,zhr);
        contentValues.put(Col_4,assr);
        contentValues.put(Col_5,mgrb);
        contentValues.put(Col_6,isa);
        long success = db.insert(Tbname,null,contentValues);
        if(success == -1)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public boolean UpdateData(String id,String n1, String n2, String n3,String n4,String n5)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(Col_1,id);
        contentValues.put(Col_2,n1);
        contentValues.put(Col_3,n2);
        contentValues.put(Col_4,n3);
        contentValues.put(Col_3,n4);
        contentValues.put(Col_4,n5);

        db.update(Tbname,contentValues,"ID = ?", new String[]{id});
        return true;
    }

    public Cursor getAllDataForNamaz()
    {
        openDatabase();
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cs = db.rawQuery("SELECT * FROM "+ Tbname2 + " WHERE date_for = "+ date(),null);
        closeDatabase();
        return cs;
    }
    public Date date()
    {
        long millis=System.currentTimeMillis();
        java.sql.Date date=new java.sql.Date(millis);
        return date;
    }

    public Cursor getAllData()
    {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cs = db.rawQuery("SELECT * FROM  "+ Tbname,null);
        return cs;
    }



    public Cursor getAllData1()
    {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cs = db.rawQuery("SELECT ID FROM  "+ Tbname,null);
        return cs;
    }
}

输出:

group1 : {.5,.5,5}
group2 : { 3 }
group3 : { 2 }
group4 : { 1 }
group5 : { 1.1}
k1 = 2.5 and k2 =3.5 Group size = 3.

possible solutions : (.5,1.1,1),(.5,.5,2),(.5,1,2). . 
  

Lorem ipsum dolor坐着,Word1轻松自如。 unc   贝母,贝母和甜菜。

     

毛里斯nec maximus purus。 Maecenas坐在a特鲁。 Praesent   Word3 sed rhoncus eo。 Duis id commodo orci。

     

dignissim lacus的Quisque。

1 个答案:

答案 0 :(得分:1)

尝试:

all_words = word.split()
print(all_words)

split返回一个list,它不会更改原始的string

更新(以使所有单词集中在一个列表中):

RanTxtString = open("randomText.txt", "r")
all_words=[]
for word in RanTxtString:
    all_words = all_words+word.split()
print(all_words)