标签: file-io

为什么我的文件被覆盖?

在python中使用此代码时:

 f = open('ping.log', 'r+')
 f.write("["+time.ctime()+"]"+"Status")
 f.close()
Run Code Online (Sandbox Code Playgroud)

我的文件总是被覆盖.并且只有一行,如下所示:

[2011年9月2日星期五16:30:56]状态

为什么会被覆盖?

python file-io

0
推荐指数
1
解决办法
205
查看次数

从位置读取文件

FileStream infile = new FileStream(@"C:\Users\John\Desktop\ProjectNew\nov.txt",     FileMode.Open, FileAccess.Read);
        int position = x.Length;
        infile.Seek(position, SeekOrigin.Begin);
Run Code Online (Sandbox Code Playgroud)

但是Seek方法返回数字.如何从字符串中的位置读取文件"infile"?

c# file-io

0
推荐指数
1
解决办法
1万
查看次数

如何从文件读取到数组

我正在尝试从文件读取数组.我尝试了两种不同的风格,两种都不起作用.以下是两种风格.

风格1

public class FileRead {

        int i;
        String a[] = new String[2];
        public void read() throws FileNotFoundException {
            //Z means: "The end of the input but for the final terminator, if any"

            a[i] = new Scanner(new File("C:\\Users\\nnanna\\Documents\\login.txt")).useDelimiter("\\n").next();
           for(i=0; i<=a.length; i++){
            System.out.println("" + a[i]);
           }


        }

        public static void main(String args[]) throws FileNotFoundException{
            new FileRead().read();

        }
    }
Run Code Online (Sandbox Code Playgroud)

风格2

public class FileReadExample {
    private int j = 0;
    String path = null;

    public void fileRead(File file){
StringBuilder attachPhoneNumber = new StringBuilder(); …
Run Code Online (Sandbox Code Playgroud)

java arrays file-io

0
推荐指数
1
解决办法
268
查看次数

PostgreSQL:从文件系统加载文件而不插入表

我该怎么做?
在mysql中,我这样做:

SELECT LOAD_FILE('/path/to/file');
Run Code Online (Sandbox Code Playgroud)

那么postgres呢?不使用\copypsql命令?

mysql postgresql file-io

0
推荐指数
1
解决办法
1624
查看次数

从文件读取到列表时是否有更短的方法来删除换行符?

这是我目前的代码:

dfile = open('dictionary.txt', 'r')
sfile = open('substrings.txt', 'r')
dictionary_words = []
substrings = []

for line in dfile:
    dictionary_words.append(line.rstrip('\n'))
for line in sfile:
    substrings.append(line.rstrip('\n'))
Run Code Online (Sandbox Code Playgroud)

它有效,但看起来很罗嗦.

这是写出来的合适方式,还是我错过了一个更简单的过程?

python file-io whitespace list strip

0
推荐指数
1
解决办法
3771
查看次数

如何从文本文件中的每一行中获取一个int?

我有一个年份列表:

1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
Run Code Online (Sandbox Code Playgroud)

我试图找出如何从每一行抓住年份..

我一直在网上阅读,到目前为止我读了getline,但我想这不会起作用,因为它只适用于字符串.

我还能用什么?

PS.这是我的代码

int main(int argc, char *argv[]) {

    string line;
    ifstream myfile ("leapin.txt");
    if (myfile.is_open()){
        while ( myfile.good() ){
            getline (myfile, line);
        }
        myfile.close();
    }

    else cout << "Unable to open file";

      system("PAUSE");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ int file-io

0
推荐指数
1
解决办法
153
查看次数

Python - 读取文件并通过分隔符分隔行的最佳方法

读取文件并通过分隔符划分行的最佳方法是什么.返回的数据应该是元组列表.

这种方法可以被打败吗?这可以更快/使用更少的内存吗?

def readfile(filepath, delim):
    with open(filepath, 'r') as f:
        return [tuple(line.split(delim)) for line in f]
Run Code Online (Sandbox Code Playgroud)

python file-io generator

0
推荐指数
1
解决办法
8772
查看次数

Python编程作业

首先,谢谢你的时间和答案.我的任务是让我的程序打开一个文本文件,读取它的数据,这样每个单词都是一个不同的字符串,并创建一个HTML文档,将每个字符串显示为随机颜色.因此,它几乎要求我们从文本文件中取出每个单词,将每个单词更改为随机颜色,并从中创建HTML文档.这是我到目前为止的代码:

import random  
def main():
    filename = input("Enter Text File:") 
    infile = open(filename, "r")
    filename2 = input("Enter HTML Document:")
    outfile = open(filename2, "w")
    print("<html>", file=outfile)
    print("  <head>", file=outfile)
    print("  </head>", file=outfile)
    print("  <body>", file=outfile)
    filestring = infile.read()
    file = filestring.split()
    filelength = len(file)
    num = int(random.uniform(0,256))
    num1 = int(random.uniform(0,256))
    num2 = int(random.uniform(0,256))
    i = 0


    for i in range(filelength):
        r = num
        g = num1
        b = num2
        rgb = "{0:02X}{1:02X}{2:02X}".format(r, g, b)
        print('    <span style="color:#{0}">{1}</span>'.format(rgb,                    file[i]),file=outfile)
        i = 0 …
Run Code Online (Sandbox Code Playgroud)

html python file-io

0
推荐指数
1
解决办法
254
查看次数

解析文本文件时抛出std :: out_of_range

我有以下代码来读取文本文件.

const string FILENAME = PACKAGES_DIR + pname;
  //the arguments to ifstream is a cstring and hence the conversion must be made
  ifstream freader;
  freader.open(FILENAME.c_str(),ios::in);
  if(freader.is_open())
  {
    while(freader.good())
    {
      string line;
      getline(freader,line);
      cout<<line<<endl;
      if(line.find("PackageId:"))
      {
        cout<<line.substr(11)<<endl;
      }
      else if(line.find("Name:"))
      {
        cout<<line.substr(5)<<endl;
      }
      else if(line.find("Version:"))
      {
        cout<<line.find(8)<<endl;
      }
      else
      {
        cout<<line<<endl;
      }

    }
  }
Run Code Online (Sandbox Code Playgroud)

有问题的文本文件的内容是

PackageId:994
Name:basket
Version:1.80-1
Deps:kdebase-runtime,libc0.1,libc0.1-udeb,libc6,libc6-udeb,libc6.1,libc6.1-udeb,libgcc1,libgpg-error0,libgpgme11,libkdecore5,libkdeui5,libkfile4,libkio5,libkparts4,libkutils4,libphonon4,libqimageblitz4,libqt4-dbus,libqt4-network,libqt4-qt3support,libqt4-svg,libqt4-xml,libqtcore4,libqtgui4,libstdc++6,libunwind7,libx11-6,phonon
Run Code Online (Sandbox Code Playgroud)

我得到的输出是

PackageId:994
geId:994
Name:basket

Version:1.80-1
0-1
Deps:kdebase-runtime,libc0.1,libc0.1-udeb,libc6,libc6-udeb,libc6.1,libc6.1-udeb,libgcc1,libgpg-error0,libgpgme11,libkdecore5,libkdeui5,libkfile4,libkio5,libkparts4,libkutils4,libphonon4,libqimageblitz4,libqt4-dbus,libqt4-network,libqt4-qt3support,libqt4-svg,libqt4-xml,libqtcore4,libqtgui4,libstdc++6,libunwind7,libx11-6,phonon
e-runtime,libc0.1,libc0.1-udeb,libc6,libc6-udeb,libc6.1,libc6.1-udeb,libgcc1,libgpg-error0,libgpgme11,libkdecore5,libkdeui5,libkfile4,libkio5,libkparts4,libkutils4,libphonon4,libqimageblitz4,libqt4-dbus,libqt4-network,libqt4-qt3support,libqt4-svg,libqt4-xml,libqtcore4,libqtgui4,libstdc++6,libunwind7,libx11-6,phonon

terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr
Run Code Online (Sandbox Code Playgroud)

我想要的输出是:

PackageId:994
994
Name:basket …
Run Code Online (Sandbox Code Playgroud)

c++ file-io text

0
推荐指数
1
解决办法
1082
查看次数

存储指向类对象的指针的地址的最佳方法

我有一个A类,它的对象是动态创建的:

A *object;
object = new A;
Run Code Online (Sandbox Code Playgroud)

在执行中会有许多A对象.我在A中创建了一个方法,根据传递的id返回特定对象的地址.

A *A::get_obj(int id)
Run Code Online (Sandbox Code Playgroud)

get_obj的实现需要itteration,所以我选择了向量来存储对象的地址.

vector<A *> myvector
Run Code Online (Sandbox Code Playgroud)

我认为另一种方法是创建一个文件并将地址存储为特定行上的文本(这将是id).这将帮助我减少内存使用量,因为我不会创建一个向量.我不知道的是,这种方法会比矢量方法消耗更多的时间吗?任何其他做同样的选择都是受欢迎的.

c++ file-io vector

0
推荐指数
1
解决办法
158
查看次数

标签 统计

file-io ×10

python ×4

c++ ×3

arrays ×1

c# ×1

generator ×1

html ×1

int ×1

java ×1

list ×1

mysql ×1

postgresql ×1

strip ×1

text ×1

vector ×1

whitespace ×1