我有两个列表已经按需要排序,我需要将它们放入一个文件中,如下例所示:
list1 = [a, b, c, d, e]
list2 = [1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)
输出文件应如下所示:
a1
b2
c3
d4
e5
Run Code Online (Sandbox Code Playgroud)
我是相当新的python,所以我真的不知道怎么做文件写.我读取使用with open(file, 'w') as f:是一种更好/更简单的方式来启动写入块,但我不确定如何合并列表并打印它们.我可以将它们合并到第三个列表中并将其打印到文件中,print>>f, item但我想看看是否有更简单的方法.
谢谢!
延迟编辑:查看我的列表,它们的长度不一样,但所有数据都需要打印.因此,如果list2转到7那么输出将需要是:
a1
b2
c3
d4
e5
6
7
Run Code Online (Sandbox Code Playgroud)
反之亦然,其中list1可能比list2长.
我有这个函数遍历一个目录,应该读入每个文件并将其写入生成的HTML文件.BufferedReader应该正确读取,因为我在其他地方使用相同的东西.但是,在生成的HTML文件中,我只从目录中的原始文件获取所有其他数据行.这是应该完成此任务的方法:
// Tests to see if "File" is actually a directory or file,
// then writes out the file if it passes the test
void writeFiles(File directory, BufferedWriter bw) {
try{
for( File file : directory.listFiles() ){
if(!file.isDirectory()) {//is a file lets read it
FileInputStream filestream = new FileInputStream(file);
DataInputStream in = new DataInputStream(filestream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String buff = new String();
bw.write("<b>////////////////////////////////</b><br/>");
bw.write("<b>File: " + file.getName() + "</b><br/>");
bw.write("<b>////////////////////////////////</b><br/>");
while((buff=br.readLine()) != null){
bw.write(br.readLine() + "<br/>");
} …Run Code Online (Sandbox Code Playgroud) 目前我正在使用此代码
if __name__=="__main__":
try:
main()
except KeyboardInterrupt:
f.close
print "left!"
Run Code Online (Sandbox Code Playgroud)
这是最好的方法吗?在脚本的早期,正在写入一个文件,我想确保在脚本终止时干净地关闭它.有什么意见吗?
假设我的Visual Studio C#Web应用程序中有一个像ClientBin这样的文件夹.
我很困惑@""将在以下试图创建目录路径的C#代码中执行什么操作:
String DirectoryPath = System.IO.Path.Combine(@"" + "ClientBin");
Run Code Online (Sandbox Code Playgroud)
或者说到fa文件,如下例所示:
String FilePath = System.IO.Path.Combine("admin", "access", @"" + "users.aspx");
Run Code Online (Sandbox Code Playgroud)
基本上,有人可以向我解释@""与System.IO.Path.Combine方法有什么关系吗?
当我尝试编译这个程序时,我在第21行得到一个"无法访问的语句"错误:
import java.util.*;
import java.io.*;
import java.nio.file.*;
import java.lang.StringBuilder;
class FilePrep {
public static void main(String args[]) {
}
public String getStringFromBuffer() {
try {
Path file = Paths.get("testfile2.txt");
FileInputStream fstream = new FileInputStream("testfile2.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String inputLine = null;
StringBuffer theText = new StringBuffer();
while((inputLine=br.readLine())!=null) {
theText.append(inputLine+" ");
}
return theText.toString();
System.out.println(theText); // <-- line 21
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
return null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
完整的编译器输出是:
Main.java:21: error: …Run Code Online (Sandbox Code Playgroud) 我是C的新手.我是C中的一个文件.但是当我试图在代码下运行时,我得到的内存错误不够.我无法理解这个问题.我清除了我的临时文件夹,但仍然存在错误.请帮忙.
File *logfile;
logfile = fopen ("sample.txt","a+");
printf("\n logfile=%d\n",logfile);
if (!logfile)
{
perror("Error");
exit(EXIT_FAILURE);
}
Run Code Online (Sandbox Code Playgroud) 您好,我正在尝试找到一种方法将2个变量从我的工作区保存到文件中.我希望使用原始日期和当前日期将文件名放在一起.
我只想要变量的最大值,所以:
max(streaking)
Run Code Online (Sandbox Code Playgroud)
和
max(tap_total)
Run Code Online (Sandbox Code Playgroud)
原始文件名是:
3_FM001_02_05460 $ bandp指向$ 64_24000_FWD_1x1_PRI_PRI_PRI_PRI_15_17_ActivePixelMeans.csv
我想要使用的原始文件名的唯一部分是:
3_FM001_02_05460$BandP$64_24000_FWD_1x1
Run Code Online (Sandbox Code Playgroud)
这些可以保存在文本文件或电子表格中,没关系.
新文件名的示例如下所示:
3_FM001_02_05460$BandP$64_24000_FWD_1x1_7-26-2012
Run Code Online (Sandbox Code Playgroud)
也,
如果可以在文件中执行某些操作以显示哪个变量,例如:
Streaking: 1.272 % this would come from the variable max(streaking)
Tap_Total: 2.252 % this would come from the varaible max(tap_total)
Run Code Online (Sandbox Code Playgroud)
编辑:
% Construct a questdlg with three options
choice = questdlg('Would you like to save?', ...
'Save Options', ...
'Yes','No','Cancel','Cancel');
% Handle response
switch choice
case 'Yes'
disp([choice ' processing.'])
save_option = 1;
case 'No'
disp([choice ' processing.'])
save_option = 0;
case 'Cancel' …Run Code Online (Sandbox Code Playgroud) 我第一次玩Python,并且手上有一个迷你项目,我正在忙着掌握这种语言.我基本上有兴趣知道我是否想要一个好的解决方案,或者是否有更好的方法去做.
有一个CSV文件基本上列出了整数(或应列出整数),每行约有三个整数,用逗号分隔.我这样做的方法是:
这是非常直接的,从技术上来说,"做到了".但是,在我阅读了一些建议使用pickle模块来读取由整数组成的文件的Python文档后,我的思绪发生了冲突.
显然,如果正在读取readline,则每行都以字符串形式读入.这意味着,整数也被作为字符串读入,这里可能会有精度损失?Pickle是一个神奇的模块,可以将所有内容转换为字符串表示形式,然后可以将其转换回原始数据类型表示而不会丢失精度.
恕我直言,我的程序代码在没有使用pickle模块的情况下工作正常,但是为了编写好的代码,你们有什么建议以及它背后的理由是什么:
我是matlab上的新手并做了一些小的图像处理任务.我正在使用imread来阅读一些图像但是在阅读图像后我没有得到什么样的表示.它是图像直方图的二进制表示吗?
如果有人可能形容我,我将不胜感激:)
这是我从文本文件中读取的代码:
public RecordManager(){
int pos;
String athlete, record, country, raceTime;
try {
Scanner scFile = new Scanner(new File("resultdata.txt"));
while(scFile.hasNext()){
Scanner sc = new Scanner(scFile.next()).useDelimiter("#");
athlete = sc.next();
country = sc.next();
pos = sc.nextInt();
record = sc.next();
raceTime = sc.next();
sc.close();
if("WRC".equals(record)){
resultArr[size] = new WorldRecord(athlete, country, pos, raceTime);
}
else if("OLR".equals(record)){
resultArr[size] = new OlympicRecord(athlete, country, pos, raceTime);
}
else{
resultArr[size] = new RaceResult(athlete, country, pos, raceTime);
}
size++;
}
} catch (FileNotFoundException ex) {
Logger.getLogger(RecordManager.class.getName()).log(Level.SEVERE, null, ex);
}
Run Code Online (Sandbox Code Playgroud)
这是文本文件中的内容: …