我正在尝试将我的xls文件输出重定向到流,但我遇到了错误。
以下是我正在使用的代码
#!/usr/bin/env python
import codecs
import datetime
import decimal
import csv
import sys
import xlrd
def main(self):
# We need to do this dance here, because we aren't writing through agate.
if six.PY2:
stream = codecs.getwriter('utf-8')(self.output_file)
else:
stream = self.output_file
def dump_xls(data,active_worksheet):
active_worksheet.append(data)
file_name = "temp.xls"
reader = csv.reader(self.input_file,delimiter=",")
workbook = openpyxl.Workbook()
worksheet = workbook.active
for i in reader:
dump_xls(i,worksheet)
workbook.save(file_name)
def launch_new_instance():
utility = CSVXLS()
utility.run()
if __name__ == '__main__':
launch_new_instance
()
我已经有输入流和所有流信息。我怎样才能将输出文件转换为流,以便数据在输出处可见。
我是python的新手,非常感谢任何帮助。
非常感谢!!!
答案 0 :(得分:0)
我不知道我明白你在寻找什么StringIO
,但是import StringIO
...
...
s_io = StringIO.StringIO() # Declare stream
for i in reader:
dump_xls(i,worksheet)
s_io.write(i) # Write to stream.
也是一个流,你可以像
s_io
最后,stream
代表 CustomListAdapter(Context context, ArrayList<String> subjects) {
super(context, R.layout.custom_list_view, subjects);
}
@NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
View customView = layoutInflater.inflate(R.layout.custom_list_view, parent, false);
singleSubject = getItem(position);
TextView singleText = (TextView) customView.findViewById(R.id.listSubjectsMyCourses);
colorLayout = (LinearLayout)customView.findViewById(R.id.colorForSubjects);
relativeLayout = (RelativeLayout)
customView.findViewById(R.id.relativeForView);
parentLayout = (RelativeLayout)
customView.findViewById(R.id.parentLayout);
points = new ArrayList<>();
selected = new ArrayList<>();
selectedsttring = new ArrayList<>();
customView.findViewById(R.id.textViewForGrades);
tinyDB = new TinyDB(getContext());
spinnerForGradePoints = (Spinner)customView.findViewById(R.id.spinnerForGrades);
final ArrayAdapter<String> gradePointAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_dropdown_item_1line, UserCourseSelection2.userSubjectGradePoint);
spinnerForGradePoints.setAdapter(gradePointAdapter);
spinnerForGradePoints.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
selectedItempos = adapterView.getSelectedItemPosition();
String getSelectedItem = adapterView.getItemAtPosition(i).toString();
tinyDB.putInt("selected", selectedItempos);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
singleText.setText(singleSubject);
colorLayout.setBackgroundColor(UserCourseSelection2.userSubjectsListColor.get(position));
int getSelected = tinyDB.getInt("selected");
spinnerForGradePoints.setSelection(getSelected);
return customView;
}