仍然是Python的新手,所以可能很容易回答。
我有一个用于for循环的列表。因此,列表中的每个项目都执行了一个操作,但我想为已发生的事情编写一个文件,如何在for循环中使用该变量为列表中的每个项目创建特定的文件名,所以在mo我有类似的东西;
mylist = ['hello', 'there', 'world']
for i in mylist:
outputfile = open('%i.csv', 'a')
print('hello there moon', file=outputfile)
我是否在使用%i
代表列表中的各个项目的正确轨道上?
答案 0 :(得分:8)
您可以使用format()
按照以下方式执行操作:
mylist = ['hello', 'there', 'world']
for word in mylist:
with open('{}.csv'.format(word), 'a') as f_output:
print('hello there moon', file=f_output)
使用with
之后也会自动关闭您的文件。
format()
有许多可能的功能允许所有类型的字符串格式化,但简单的情况是用{}
替换word
,在您的情况下为module Foo
def self.included(base)
overrides = instance_methods.select { |method| base.instance_method(method).owner != self }
raise "#{self.name} overrides #{overrides.join(', ')}" if overrides.any?
end
end
。
答案 1 :(得分:2)
使用以下代码。
-Wstrict-overflow=5
答案 2 :(得分:2)
您应该使用outputfile = open('%s.csv' % i, 'a')
,因为列表中的项目是字符串。
private Activity activity;
private LayoutInflater inflater;
private List<Contents> tableofcontents;
public TocAdapter(Activity activity, List<Contents> tableofcontents) {
this.activity = activity;
this.tableofcontents = tableofcontents;
}
@Override
public int getCount() {
return tableofcontents == null ? 0 : tableofcontents.size();
}
@Override
public Object getItem(int location) {
return tableofcontents.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.tableofcontents_view, null);
ImageView thumbNail = (ImageView) convertView
.findViewById(R.id.thumbnail);
TextView name = (TextView) convertView.findViewById(R.id.name);
Contents m = tableofcontents.get(position);
String id = tableofcontents.get(position).getCategory_id();
Log.d(TAG,"m Check :"+m.getCategory_id());
Log.d(TAG,"tggCheck :"+tableofcontents.get(position).getCategory_id());
if(tableofcontents.get(position).getCategory_id().equals(1)) {
Glide.with(activity)
.load(R.drawable.novels)
.placeholder(R.mipmap.ic_launcher)
.into(thumbNail);
}else if(tableofcontents.get(position).getCategory_id().equals(2)){
Glide.with(activity)
.load(R.drawable.verses)
.placeholder(R.mipmap.ic_launcher)
.into(thumbNail);
}else if(tableofcontents.get(position).getCategory_id().equals(3)) {
Glide.with(activity)
.load(R.drawable.songs)
.placeholder(R.mipmap.ic_launcher)
.into(thumbNail);
}else if(tableofcontents.get(position).getCategory_id().equals(4)) {
Glide.with(activity)
.load(R.drawable.stories)
.placeholder(R.mipmap.ic_launcher)
.into(thumbNail);
}else if(tableofcontents.get(position).getCategory_id().equals(5)) {
Glide.with(activity)
.load(R.drawable.plays)
.placeholder(R.mipmap.ic_launcher)
.into(thumbNail);
}else if(tableofcontents.get(position).getCategory_id().equals(6)) {
Glide.with(activity)
.load(R.drawable.essay)
.placeholder(R.mipmap.ic_launcher)
.into(thumbNail);
}else if(tableofcontents.get(position).getCategory_id().equals(7)) {
Glide.with(activity)
.load(R.drawable.others)
.placeholder(R.mipmap.ic_launcher)
.into(thumbNail);
}
name.setText(m.getName());
return convertView;
}
答案 3 :(得分:1)
mylist = ['hello', 'there', 'world']
for item in mylist:
with open('%s.txt'%item,'a') as in_file:
in_file.write('hello there moon')
答案 4 :(得分:0)
f
,并且变量位于字符串引号内,并用{}
包围。
f'{i}.csv'
mylist = ['hello', 'there', 'world']
for i in mylist:
outputfile = open(f'{i}.csv', 'a')
print('hello there moon', file=outputfile)