我有以下文字:
xml = '''
<accessibility_info>
<accessibility role="captions" available="true" />
</accessibility_info>
<crew_member billing="top"
<display_name>John Viscount</display_name>
</crew_member>
<products>
<territory>GB</territory>
</products>'''
我需要删除以下<crew_member>
块。这就是我目前正在做的事情:
clean_xml = re.sub('<crew_member>.*</crew_member>', '', metadata_contents,
flags=re.DOTALL)
但是,它还添加了换行符:
<accessibility_info>
<accessibility role="captions" available="true" />
</accessibility_info>
<products>
<territory>GB</territory>
</products>
我如何更改正则表达式以剥离换行符,所以它看起来像:
<accessibility_info>
<accessibility role="captions" available="true" />
</accessibility_info>
<products>
<territory>GB</territory>
</products>'
答案 0 :(得分:2)
试试这个
print re.sub('<crew_member([^\>]*)>.*</crew_member>\n', '', xml, flags=re.DOTALL)
答案 1 :(得分:0)
我知道这有点旧,但我想说新行实际上来自用于将新文本写入文件的方法。如果我使用public class DetailedActivity extends AppCompatActivity {
//declared variables
TextView txtName, txtEmail, txtPhone;
Bundle img;
String name;
String email;
String phone;
ImageView imageView;
Context mContext;
Bitmap bitmap;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detailed);
//Get Intent
Intent i = getIntent();
//items to receive intent
name = i.getStringExtra("name");
email = i.getStringExtra("email");
phone = i.getStringExtra("phone");
bitmap = i.getParcelableExtra("imageView");
//Assign values to layout file items
txtName = (TextView) findViewById(R.id.tvName);
txtEmail = (TextView) findViewById(R.id.tvEmail);
txtPhone = (TextView) findViewById(R.id.txtPhone);
imageView = (ImageView) findViewById(R.id.profilPic);
/**
* Set parsed Text
*/
txtName.setText(name);
txtEmail.setText(email);
txtPhone.setText(phone);
imageView.setImageBitmap(bitmap);
,则会添加新行,但如果我使用print()
,则不会添加新行。