在UWP中,RichEditBox将.rtf作为源代码加载而不是解释它

时间:2018-04-27 03:33:58

标签: c# xaml uwp

我使用RichEditBox的LoadFromStream()方法来检索我的文档,但是放入编辑器的数据是.rtf源。我将格式设置为Rtf,这不是问题。我一直这样:

import pandas as pd

#Create The DataFrame 

df = (pd.DataFrame({ 
                'opponent': ['76ers', 'blazers', 'bobcats'], 
                'nearest_neighbors': [['Zach LaVine', 'Jeremy Lin',       'Nate Robinson', 'Isaia']] * 3})
  .set_index([ 'opponent']))

#Explode The Data Frame

(pd.melt(df.nearest_neighbors.apply(pd.Series).reset_index(), 
     id_vars=[ 'opponent'],
     value_name='nearest_neighbors')
 .set_index([ 'opponent'])
 .drop('variable', axis=1)
 .dropna()
 .sort_index()
)

#Save DF to CSV

df.to_csv("Baskets.CSV")
#Load DF to CSV
df = pd.read_csv("Baskets.csv")

# Re Run Explode Code on csv 

(pd.melt(df.nearest_neighbors.apply(pd.Series).reset_index(), 
         id_vars=[ 'opponent'],
         value_name='nearest_neighbors')
 .set_index([ 'opponent'])
 .drop('variable', axis=1)
 .dropna()
 .sort_index()
 )






KeyError                                  Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2441             try:
-> 2442                 return self._engine.get_loc(key)
   2443             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'opponent'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-25-6ffdc50e8f2c> in <module>()
      1 (pd.melt(df.nearest_neighbors.apply(pd.Series).reset_index(), 
      2              id_vars=[ 'opponent'],
----> 3              value_name='nearest_neighbors')
      4      .set_index([ 'opponent'])
      5      .drop('variable', axis=1)

~/anaconda3/lib/python3.6/site-packages/pandas/core/reshape/reshape.py in melt(frame, id_vars, value_vars, var_name, value_name, col_level)
    761     mdata = {}
    762     for col in id_vars:
--> 763         mdata[col] = np.tile(frame.pop(col).values, K)
    764 
    765     mcolumns = id_vars + var_name + [value_name]

~/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py in pop(self, item)
    548         Return item and drop from frame. Raise KeyError if not found.
    549         """
--> 550         result = self[item]
    551         del self[item]
    552         try:

~/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
   1962             return self._getitem_multilevel(key)
   1963         else:
-> 1964             return self._getitem_column(key)
   1965 
   1966     def _getitem_column(self, key):

~/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py in _getitem_column(self, key)
   1969         # get column
   1970         if self.columns.is_unique:
-> 1971             return self._get_item_cache(key)
   1972 
   1973         # duplicate columns & possible reduce dimensionality

~/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py in _get_item_cache(self, item)
   1643         res = cache.get(item)
   1644         if res is None:
-> 1645             values = self._data.get(item)
   1646             res = self._box_item_values(item, values)
   1647             cache[item] = res

~/anaconda3/lib/python3.6/site-packages/pandas/core/internals.py in get(self, item, fastpath)
   3588 
   3589             if not isnull(item):
-> 3590                 loc = self.items.get_loc(item)
   3591             else:
   3592                 indexer = np.arange(len(self.items))[isnull(self.items)]

~/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2442                 return self._engine.get_loc(key)
   2443             except KeyError:
-> 2444                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2445 
   2446         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'opponent'

帮助!

1 个答案:

答案 0 :(得分:0)

LoadFromStream方法应该与正确的.rtf格式文档配合使用。由于您没有提供任何代码段,因此这是我可以正确检索文档的简单代码:

<RichEditBox x:Name="Richbox"  Height="400" Margin="40" ></RichEditBox>
<Button x:Name="Loadfile" Content="Load File" Click="Loadfile_Click"></Button>

背后的代码

private async void Loadfile_Click(object sender, RoutedEventArgs e)
{  
    StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/test1.rtf")); 
    IRandomAccessStream randAccStream = await file.OpenAsync(FileAccessMode.Read);
    Richbox.Document.LoadFromStream(TextSetOptions.FormatRtf, randAccStream);
}

如果代码本身没有问题,那么如果文档正确Rich Text Format (RTF),您可能需要检查RTF规范。带有后缀.rtf的文档可能无法确定正确的RTF格式。您可以尝试使用验证工具或只是单词来检查您的文档是否可以正确显示。

Here是我测试的RTF文件,可以很好地运行,您可以用于测试以缩小您的问题,此项目也是一个完整的演示,可以很好地运行。如果您仍有问题,请上传最小的转载项目。