用数字来分割字符串

时间:2019-05-24 04:32:56

标签: python string split

我有这个字符串

a = "IN 744301 Mus Andaman & Nicobar Islands   01  Nicobar 638 Carnicobar 9.2333  92.7833 4"

我想用正则表达式将其拆分为数字,输出将是这样

['IN' , '744301', 'Mus Andaman & Nicobar Islands', '01' , 'Nicobar', '638', 'Carnicobar', '9.2333','92.7833', '4' ]

3 个答案:

答案 0 :(得分:4)

您可以使用先行和后行:

import re
a = "IN 744301 Mus Andaman & Nicobar Islands   01  Nicobar 638 Carnicobar 9.2333  92.7833 4"
new_a = re.split('(?<=\d)\s+|\s+(?=\d)', a)

输出:

['IN', '744301', 'Mus Andaman & Nicobar Islands', '01', 'Nicobar', '638', 'Carnicobar', '9.2333', '92.7833', '4']

正则表达式说明:

(?<=\d)\s+:匹配任何以数字(\s)开头的空白(\d)。

\s+(?=\d):匹配所有空格后跟一个数字。

|:应用具有匹配项的任一联接表达式。

答案 1 :(得分:1)

您可以split用类似数字的样式,然后findall用相同的样式。由于splitfindall是“姐妹”函数,因此您将获得非数字和数字部分。现在,将它们压缩到一个列表中并消除空格。

from itertools import chain
# You can improve the regex to cover numbers that start with a .
NUMBER = r'\d+(?:\.\d*)?'  
combined = chain.from_iterable(zip(re.split(NUMBER, a),                                                        
                                   re.findall(NUMBER, a)))
result = [x for x in map(str.strip, combined) if x]
#['IN', '744301', 'Mus Andaman & Nicobar Islands', '01', 'Nicobar',
# '638', 'Carnicobar', '9.2333', '92.7833', '4']

答案 2 :(得分:1)

您可以将re.split与组(捕获括号)一起使用,以将定界符(数字)保留在结果中:

  private void frmRepoPremier_Load(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            string query = "select * from Repos";

            try
            {
                MySqlConnection sqlConnection = new MySqlConnection(MyConnectionString);
                MySqlCommand sqlCommand = new MySqlCommand(query, sqlConnection);
                MySqlDataAdapter sqlDataAdapter = new MySqlDataAdapter(sqlCommand);
                sqlConnection.Open();
                sqlDataAdapter.Fill(ds);
                sqlConnection.Close();
                dgvBuildings.DataSource = ds.Tables[0];

                DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
                dgvBuildings.Columns.Add(btn);
                btn.HeaderText = "Photos";
                btn.Text = "View";
                btn.Name = "btn";
                btn.UseColumnTextForButtonValue = true;
               //HID THIS COLUMN TO REPLACE THE VIEW BUTTON
                this.dgvBuildings.Columns[11].Visible = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex, "Error: Load Data", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {
                if (connection.State == ConnectionState.Open)
                { connection.Close(); }

            }
        }

        private void dgvBuildings_CellClick(object sender, DataGridViewCellEventArgs e)
        { var senderGrid = (DataGridView)sender;
          if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
          { 
          //OPEN IMAGE FROM BUTTON
          System.Diagnostics.Process.Start(textBox1.Text);
          }

        }

        private void dgvBuildings_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewRow row = this.dgvBuildings.Rows[e.RowIndex];
                textBox1.Text = row.Cells[12].Value.ToString();

                try
                {
                    if (row.Cells[12].Value != null)

                    { pictureBox1.LoadAsync(row.Cells[12].Value.ToString()); }
                    else
                    { return; }
                }
                catch (Exception)
                {
                    pictureBox1.Image = null;
                    return;
                }

            }
        }